1.11M Members

nextLine() after nextInt() or nextDouble gives trouble

 
0
 

Hi,
I'm new to this Java world, and just can't figure out this problem. After I use nextInt or nextDouble, and try to use nextLine, it won't work unless I put nextLine twice. What's the problem there, and what would be the good way to handle this?

Just gave a code to illustrate a simple example I can understand.

import java.util.Scanner;

public class nextLineProblem {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Type in something for line 1");
		String line1 = sc.nextLine();

		System.out.println("Type an integer for line 2");
		int line2 = sc.nextInt();
		
		System.out.println("Type in something for line 3");
		String line3 = sc.nextLine();
		
		System.out.println("Line1: " + line1 + "\n" + "Line2: " + line2 + "\n" + "Line3:" + line3);
		
	}

}
 
0
 

I had this problem before also.

this is what I came across:

http://www.robocommunity.com/blog/entry/12176/Java-Quick-Tip---Problems--with-the-Scanner-Class/

For my assignment a while back , I just used the .next() to read the next input. But my assignment only required a single word , not separated by spaces (so i could just use tokens).

looks like you're just going to have to add the extra line as far as I can tell.

 
4
 

The reason it gives you trouble is because when the user enters an integer then hits enter, two things have just been entered - the integer and a "newline" which is \n. The method you are calling, "nextInt", only reads in the integer, which leaves the newline in the input stream. But calling nextLine() does read in newlines, which is why you had to call nextLine() before your code would work. You could have also called next(), which would also have read in the newline.

 
0
 

Just change the delimiter.

Scanner in = new Scanner(System.in);
in.useDelimiter("\n");
 
0
 

Thanks, now I understand what's going on now and how to deal with it when using the Scanner class.
Just left pondering if it's a bug or a feature :)

 
0
 

A new problem came to mind today.
It's easy to make a way around this problem if I know I'm going to have it, but what if it's unknown that I'll have a nextLine() after nextInt() , how would I be able to deal with the problem then?
For instance if there's a random function involved. Would I just have to scrap the nextLine() and go with something else?

 
1
 

Check out the documentation for the scanner class.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

It will throw "NoSuchElementException" if no line was found. If you don't haven't learned about exception handling yet, I would use "hasNext".

int myInt = 0;
String myString = "";

Scanner in = new Scanner(System.in);
in.useDelimiter("\n");

if (in.hasNextInt()){
     //read in int value
     myInt = in.nextInt();
}//if
else if (in.hasNext()){
     //read in String value
     myString = in.next();
}//else if
Question Answered as of 4 Years Ago by cgeier, jmaat7 and BestJewSinceJC
 
0
 

No, haven't learned about exception handling yet, but I'll read up on that now. Thanks for pointing it out.

 
0
 

THANKS SO MUCH FOR POSTING!!! I encountered the same problem and was getting so frustrated with the program not performing like I expected it to.
Sure enough, putting in nextLine() twice solved the problem.
I think that was is happening is that the nextInt and nextDouble are not properly terminating the allocated line of memory, so that when the nextLine() is run it is actually terminating the line that already has a value in it -- entered from the previous nextInt or nextDouble -- rather than taking in a new string readline value.

 
0
 

I am new to programming too,however, i saw some flaws on it

you dont put String line1,line2line3;

that is why the above codes don't work becs. its missing something.

I just share my knowledge as a beginner in java too..

 
0
 

i have try some practice and found that whatever nextline() you hava,only the last one can't use.and i don't understand that.

 
0
 

It's a real problem with Scanner's design - so many people fall into this trap.
You have some input with an int followed by some text, eg

101
John Doe

... and you try to read it with

int num = scanner.nextInt();
String name = scanner.nextLine();

... and name is an empty String ("")!

Here's why:
Your input looks like this with the new line characters shown explicitly
101\nJohn Doe\n
nextInt takes the int value from the scanner, and stops when it finds a chaacter that's not part of an int, so it takes the "101", leaving this in the scanner
"\nJohn Doe\n"
then nextLine takes everything up to the first \n character - a zero-length String ("").

Possible fixes:
1. Add add extra nextLine() between the nextInt and the real nextLine to clear the unwanted \n. This may be difficult if the nextInt and the nextLine are in different areas of code which are not always executed together.
2. Give up on nextInt (etc) and just read whole lines and parse then into ints (etc) with Integer.parseInt (etc) - in which case you can junk the whole scanner and use a BufferedReader instead.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article