| | |
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:
Solved Threads: 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.
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)
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); } }
•
•
Join Date: Sep 2009
Posts: 23
Reputation:
Solved Threads: 3
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.
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.
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
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.
•
•
Join Date: Oct 2008
Posts: 103
Reputation:
Solved Threads: 14
0
#4 Oct 21st, 2009
Just change the delimiter.
Java Syntax (Toggle Plain Text)
Scanner in = new Scanner(System.in); in.useDelimiter("\n");
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 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
For instance if there's a random function involved. Would I just have to scrap the
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? •
•
Join Date: Oct 2008
Posts: 103
Reputation:
Solved Threads: 14
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".
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)
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
![]() |
Similar Threads
- Help in do-while loop? do not return main body? (Java)
- Java program - user input and default argument (Java)
- Inventory program Pt3 (Java)
- Payroll Program (Java)
- Program help needed (Java)
- trouble with looping (Java)
- Simple Scanner Class Question (Java)
- Couple of errors in my code (Java)
- Source Code that don't work? (Java)
- Hey check out my prog. It has an error can u find it? (Java)
Other Threads in the Java Forum
- Previous Thread: inheritance override value
- Next Thread: Illegal start of expression error.
Views: 756 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






