We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,688 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

nextLine() after nextInt() or nextDouble gives trouble

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);
		
	}

}
5
Contributors
8
Replies
3 Years
Discussion Span
3 Months Ago
Last Updated
17
Views
Question
Answered
Isky
Newbie Poster
4 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

jmaat7
Light Poster
30 posts since Sep 2009
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0

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.

BestJewSinceJC
Posting Maven
2,774 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Skill Endorsements: 13

Just change the delimiter.

Scanner in = new Scanner(System.in);
in.useDelimiter("\n");
cgeier
Junior Poster
104 posts since Oct 2008
Reputation Points: 41
Solved Threads: 15
Skill Endorsements: 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 :)

Isky
Newbie Poster
4 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

Isky
Newbie Poster
4 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
cgeier
Junior Poster
104 posts since Oct 2008
Reputation Points: 41
Solved Threads: 15
Skill Endorsements: 0
Question Answered as of 3 Years Ago by cgeier, BestJewSinceJC and jmaat7

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

Isky
Newbie Poster
4 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

morrisga2000
Newbie Poster
1 post since Mar 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.1074 seconds using 2.75MB