#quick question about input
1 messages · Page 1 of 1 (latest)
show your code
for(int i = 0; i < 3; i++) {
System.out.println("Name: ");
name = scanner.next();
System.out.println("Address: ");
address = scanner.nextLine();
System.out.println("Age: ");
age = scanner.next();
pw.println(name + ", " + address + ", " + age);
}
@vocal ermine
@topaz pumice
Mixing any nextXXX method with nextLine from the Scanner class for user input, will not ask you for input again but instead result in an empty line read by nextLine.
To prevent this, when reading user input, always only use nextLine. If you need an int, do
int value = Integer.parseInt(scanner.nextLine());
instead of using nextInt.
Assume the following:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Hello " + name + ", you are " + age + " years old");
When executing this code, you will be asked to enter an age, suppose you enter 20.
However, the code will not ask you to actually input a name and the output will be:
Hello , you are 20 years old.
The reason why is that when you hit the enter button, your actual input is
20\n
and not just 20. A call to nextInt will now consume the 20 and leave the newline symbol \n in the internal input buffer of System.in. The call to nextLine will now not lead to a new input, since there is still unread input left in System.in. So it will read the \n, leading to an empty input.
So every user input is not only a number, but a full line. As such, it makes much more sense to also use nextLine(), even if reading just an age. The corrected code which works as intended is:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age:");
// Now nextLine, not nextInt anymore
int age = Integer.parseInt(scanner.nextLine());
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Hello " + name + ", you are " + age + " years old");
The nextXXX methods, such as nextInt can be useful when reading multi-input from a single line. For example when you enter 20 John in a single line.
so how do i take an input with a space
@vocal ermine
for example
123 slow street
as an address
did you read it ?
it doesn't matter
the message is explaining hat you should not use any next method outside of nextLine
what do you not understand here ?
yes
i tried that and it didnt work
show your code
for(int i = 0; i < 3; i++) {
System.out.println("Name: ");
name = scanner.nextLine();
System.out.println("Address: ");
address = scanner.nextLine();
System.out.println("Age: ");
age = scanner.nextLine();
pw.println(name + ", " + address + ", " + age);
}
would i have to make the last input address
so that it doesnt skip
and i could use nextLine for the last input
always use nextLine
it doesnt skip with nextline. not sure why u think that
so why does it skip
what's your input ?
it puts the full like into ur first input
so what's the input you are giving ?
if u want to split the input by space, either use split or actually use next() and nextint() in this particular case if u truly want to read multi input on single line
im not even inputting anything and its skipping to the next question
wdym
perhaps ur clicking enter twice without entering any text
please tell me what you are inputting
ull have to share the full code and full input/output dialog as screenshot
otherwise we can't pinpoint what ur doing wrong
and all ull get is a "but it works on our end"
so do i use the parse thing
instead of only nextLine
even if it is in a different method
so should i use a different scanner
no
i just use the parse
it likely would also happen with a different scanner, cause iirc its happening in System.in, not the scanner
and multiple scanners on the same resource can interfere
👌