String input in java - Stack Overflow
java - How to get user input with string - Stack Overflow
Getting string from input in Java - Stack Overflow
Error: java.lang.NumberFormatException: For input string: "188"
Can I use a file instead of the console to input strings?
Is it possible for me to accept several strings on a single line?
nextLine() reads everything up to and including the next newline character.
However, nextInt() only reads the characters that make up the integer, and if the integer is the last (or only) text in the line, you'll be left with only the newline character.
Therefore, you'll get a blank line in the subsequent nextLine(). The solution is to call nextLine() once before the loop (and discard its result).
Information regarding the code is mentioned in the comments written next to each line.
public static void main(String[] args) {
int num1 = sc.nextInt(); //take int input
double num2 = sc.nextDouble(); //take double input
long num3 = sc.nextLong(); //take long input
float num4 = sc.nextFloat(); //take float input
sc.nextLine(); //next line will throw error if you don't use this line of code
String str = sc.nextLine(); //take String input
}
public int getInput() {
lastName = input.nextLine();
return lastName;
}
Here you said, that getInput will return int, change it into
public String getInput() {
lastName = input.nextLine();
return lastName;
}
For more information, take a look here
In your code, you declare getInput method will return an int by:
public int getInput()
Since the name should be a String, not an int, you need to adjust your method signature:
public String getInput() {
lastName = input.nextLine();
return lastName;
}
You have to press Ctrl+z (or Ctrl+d on UNIX) to indicate that there won't be more inputs.
The standard input stream usually never ends if you do not indicate that. How could your program know, that the user does not enter another line?
So you should only read the lines that you explicitely know will get entered, like that:
Scanner input = new Scanner(System.in);
String first = input.nextLine();
String second = input.nextLine();
input.close();
System.out.println(first);
System.out.println(second);
If you want to have a variable number of lines, you should go with your while loop, but the user must indicate the end of input, as Christian suggested. Like that:
Scanner input = new Scanner(System.in);
StringBuilder builder = new StringBuilder();
while (input.hasNext()) {
builder.append(input.nextLine()).append(System.lineSeparator());
}
input.close();
System.out.println(builder.toString());
Running that will loop infinitely (seemingly). But when pressing CTRL-Z you will end the input and input.hasNext() will return false. (Note, that CTRL-Z works in Linux and Windows.)
EDIT as per Erwin Bolwidt's comment to Christian's answer:
If you are redirecting the standard input stream, so that it for example reads from a file, the EOF marker (end of file) will indicate the end of input to the scanner. So obviously no user interaction is involved and the program will nevertheless behave correctly.