944,161 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 6858
  • Java RSS
Oct 21st, 2009
0

nextLine() after nextInt() or nextDouble gives trouble

Expand Post »
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.

java Syntax (Toggle Plain Text)
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class nextLineProblem {
  5.  
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8. System.out.println("Type in something for line 1");
  9. String line1 = sc.nextLine();
  10.  
  11. System.out.println("Type an integer for line 2");
  12. int line2 = sc.nextInt();
  13.  
  14. System.out.println("Type in something for line 3");
  15. String line3 = sc.nextLine();
  16.  
  17. System.out.println("Line1: " + line1 + "\n" + "Line2: " + line2 + "\n" + "Line3:" + line3);
  18.  
  19. }
  20.  
  21. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Isky is offline Offline
4 posts
since Oct 2008
Oct 21st, 2009
0
Re: nextLine() after nextInt() or nextDouble gives trouble
I had this problem before also.

this is what I came across:

http://www.robocommunity.com/blog/en...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.
Reputation Points: 10
Solved Threads: 4
Light Poster
jmaat7 is offline Offline
29 posts
since Sep 2009
Oct 21st, 2009
3
Re: nextLine() after nextInt() or nextDouble gives trouble
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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 21st, 2009
0
Re: nextLine() after nextInt() or nextDouble gives trouble
Just change the delimiter.
Java Syntax (Toggle Plain Text)
  1. Scanner in = new Scanner(System.in);
  2. in.useDelimiter("\n");
Reputation Points: 41
Solved Threads: 15
Junior Poster
cgeier is offline Offline
104 posts
since Oct 2008
Oct 22nd, 2009
0

Thanks

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Isky is offline Offline
4 posts
since Oct 2008
Oct 22nd, 2009
0

New problem evolves

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Isky is offline Offline
4 posts
since Oct 2008
Oct 24th, 2009
1
Re: nextLine() after nextInt() or nextDouble gives trouble
Check out the documentation for the scanner class.
http://java.sun.com/j2se/1.5.0/docs/...l/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".

Java Syntax (Toggle Plain Text)
  1. int myInt = 0;
  2. String myString = "";
  3.  
  4. Scanner in = new Scanner(System.in);
  5. in.useDelimiter("\n");
  6.  
  7. if (in.hasNextInt()){
  8. //read in int value
  9. myInt = in.nextInt();
  10. }//if
  11. else if (in.hasNext()){
  12. //read in String value
  13. myString = in.next();
  14. }//else if
Reputation Points: 41
Solved Threads: 15
Junior Poster
cgeier is offline Offline
104 posts
since Oct 2008
Oct 24th, 2009
0
Re: nextLine() after nextInt() or nextDouble gives trouble
No, haven't learned about exception handling yet, but I'll read up on that now. Thanks for pointing it out.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Isky is offline Offline
4 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: inheritance override value
Next Thread in Java Forum Timeline: Illegal start of expression error.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC