#quick question about input

1 messages · Page 1 of 1 (latest)

vital skyBOT
#

Helper please have a look, thanks.

vocal ermine
#

show your code

topaz pumice
#

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

vital skyBOT
#

@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.

topaz pumice
#

so how do i take an input with a space

#

@vocal ermine

#

for example

#

123 slow street

#

as an address

vocal ermine
topaz pumice
#

yes but this is not an int

#

its a string

vocal ermine
#

it doesn't matter

topaz pumice
#

but there is never any input after in the example

#

mine there is an input after

vocal ermine
#

the message is explaining hat you should not use any next method outside of nextLine

topaz pumice
#

so what would i put

#

if i need to include spaces

vocal ermine
#

what do you not understand here ?

topaz pumice
#

so if i just use nextLine for every input it will work?

#

@vocal ermine

topaz pumice
#

i tried that and it didnt work

topaz pumice
#

this is the output

#

it skips the name input

vocal ermine
topaz pumice
#

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

vocal ermine
#

always use nextLine

dusty sedge
topaz pumice
#

so why does it skip

vocal ermine
#

what's your input ?

dusty sedge
#

it puts the full like into ur first input

vocal ermine
#

please read it

topaz pumice
#

ive read it twice

#

either im illiterate

#

or idk

vocal ermine
#

so what's the input you are giving ?

dusty sedge
#

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

topaz pumice
#

im not even inputting anything and its skipping to the next question

vocal ermine
#

wdym

topaz pumice
#

im running it

#

and instead of letting me input name

#

it skips that input

dusty sedge
#

perhaps ur clicking enter twice without entering any text

topaz pumice
#

and makes me input address

#

no

#

im not

vocal ermine
#

please tell me what you are inputting

dusty sedge
#

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"

topaz pumice
dusty sedge
#

there we go

#

look at the rest of ur code

#

ur using nextInt

topaz pumice
#

so do i use the parse thing

dusty sedge
#

instead of only nextLine

topaz pumice
#

even if it is in a different method

dusty sedge
#

its the same scanner

#

that's what counts

topaz pumice
#

so should i use a different scanner

dusty sedge
#

no

topaz pumice
#

i just use the parse

dusty sedge
#

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

topaz pumice
#

ok

#

i fixed all of it now

#

and its working

#

thanks guys

dusty sedge
#

👌

topaz pumice
#

@dusty sedge i have another question

#

once ive inputted into a txt file

#

if i want to choose a specific line to read and print out to the user