nextLine() after nextInt() or nextDouble gives trouble

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 4
Reputation: Isky is an unknown quantity at this point 
Solved Threads: 0
Isky Isky is offline Offline
Newbie Poster

nextLine() after nextInt() or nextDouble gives trouble

 
0
  #1
Oct 21st, 2009
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 23
Reputation: jmaat7 is an unknown quantity at this point 
Solved Threads: 3
jmaat7 jmaat7 is offline Offline
Newbie Poster
 
0
  #2
Oct 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
3
  #3
Oct 21st, 2009
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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 103
Reputation: cgeier is an unknown quantity at this point 
Solved Threads: 14
cgeier cgeier is offline Offline
Junior Poster
 
0
  #4
Oct 21st, 2009
Just change the delimiter.
  1. Scanner in = new Scanner(System.in);
  2. in.useDelimiter("\n");
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: Isky is an unknown quantity at this point 
Solved Threads: 0
Isky Isky is offline Offline
Newbie Poster

Thanks

 
0
  #5
Oct 22nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: Isky is an unknown quantity at this point 
Solved Threads: 0
Isky Isky is offline Offline
Newbie Poster

New problem evolves

 
0
  #6
Oct 22nd, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 103
Reputation: cgeier is an unknown quantity at this point 
Solved Threads: 14
cgeier cgeier is offline Offline
Junior Poster
 
1
  #7
Oct 24th, 2009
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".

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: Isky is an unknown quantity at this point 
Solved Threads: 0
Isky Isky is offline Offline
Newbie Poster
 
0
  #8
Oct 24th, 2009
No, haven't learned about exception handling yet, but I'll read up on that now. Thanks for pointing it out.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 756 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC