Results 1 to 5 of 5
Thread: String and Integer input bug
- 02-08-2013, 05:56 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
String and Integer input bug
Hey guys, I'm quickly trying to convert my c++ knowledge to Java and I've come across a weird bug that just wouldn't happen in C.
In my current project whenever I try to input a String value right after inputting an Int value, the system just skips over letting the user enter the String.
Heres the code I have just to reference:
Scanner keyboard1 = new Scanner(System.in);
System.out.println("Enter Int: ");
int x = keyboard1.nextInt();
System.out.println("Enter String: ");
String temp = keyboard1.nextLine();
System.out.println("Done.");
Once I've entered the value for temp, I just get output "Enter String: " and output "done" without allowance to input the String value.
Does anyone know why?
Thanks,
jktexas1
- 02-08-2013, 06:39 AM #2
Member
- Join Date
- Feb 2013
- Posts
- 4
- Rep Power
- 0
Re: String and Integer input bug
Try adding the Keyboard1.nextLine(); method so the scanner can move down a line effectively.
Java Code:import java.util.Scanner; public class Test{ public static void main( String [] args){ Scanner keyboard1 = new Scanner(System.in); System.out.println("Enter int: "); int x = keyboard1.nextInt(); System.out.println("Enter String: "); String temp = keyboard1.nextLine(); keyboard1.nextLine(); //add this line System.out.println("Done!"); } }
- 02-08-2013, 11:03 AM #3
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Re: String and Integer input bug
Hi jktexas1, welcome to the forums.
Please check the Scanner API
The method 'nextLine()' "Advances this scanner past the current line and returns the input that was skipped".
The method you want is simply 'next()'.
Regards.
- 02-09-2013, 05:46 AM #4
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Re: String and Integer input bug
Thanks so much for the help guys, the next() function fixed the problem. My data structures teacher did say that the difference between all OO languages is IO, so I'm just trying to get through that. I finally relearned file IO for java so I think I'm making a pretty smooth transition so far. Again thanks for the help, this is an awesome forum!
- 02-11-2013, 11:48 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Similar Threads
-
how can check input is double or integer?
By HearT.Hunt3r in forum New To JavaReplies: 6Last Post: 10-15-2011, 07:12 AM -
Testing if an input is an integer
By Mayday in forum New To JavaReplies: 1Last Post: 05-08-2010, 05:14 PM -
finding max and min with inheritance with two input integer
By drmarx in forum New To JavaReplies: 3Last Post: 12-04-2009, 05:21 PM -
how to know the input value of integer
By ran830421 in forum New To JavaReplies: 15Last Post: 11-18-2009, 09:01 PM -
input to an integer (simply)
By chitwood in forum Advanced JavaReplies: 3Last Post: 03-18-2009, 06:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks