Results 1 to 13 of 13
- 06-17-2010, 12:03 PM #1
Problem Of Scanner Object with its method nextLine()
Hy . . .
Here is the code . . .
problem is that i when run this program, it works smoothly until of taking integer input after that it doent ask me to give input of string for first name and skip it then ask for the lastName immediately after prompting ??Java Code:import java.util.Scanner; public class abc { public static void main ( String args[] ) { Scanner input = new Scanner( System.in ); String firstName, lastName; int num1, num2; System.out.print( "Enter first number: " ); num1 = input.nextInt(); System.out.print( "Enter second number: " ); num2 = input.nextInt(); System.out.printf( "Sum is %dn", num1 + num2 ); System.out.print( "Enter first name: " ); firstName = input.nextLine(); System.out.print( "Enter last name: " ); lastName = input.nextLine(); System.out.printf( "Your full name is %s %s", firstName, lastName ); } }
Have you know what the problem it is ??
Output like this . . .
Enter first number: 1
Enter second number: 1
Sum is 2
Enter first name: Enter last name: Storm
Your full name is Storm
Thanks . . .:)
- 06-17-2010, 12:20 PM #2
Hi,
Might be cursor is not returing to the next step after taking integer.
so ..before "Enter First name"
Try to do this below
System.out.printf( "Sum is %dn", num1 + num2 );
input.nextLine();
System.out.print( "Enter first name: " );Ramya:cool:
- 06-17-2010, 12:25 PM #3
- 06-17-2010, 12:35 PM #4
Try this code:
import java.util.Scanner;
public class Test
{
public static void main ( String args[] )
{
Scanner input = new Scanner( System.in );
String firstName, lastName;
int num1, num2;
System.out.print( "Enter first number: " );
num1 = input.nextInt();
System.out.print( "Enter second number: " );
num2 = input.nextInt();
input.nextLine();
System.out.printf( "Sum is %dn", num1 + num2 );
input.nextLine();
System.out.print( "Enter first name: " );
firstName = input.nextLine();
System.out.print( "Enter last name: " );
lastName = input.nextLine();
System.out.printf( "Your full name is %s %s", firstName, lastName );
}
}Ramya:cool:
- 06-17-2010, 01:25 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
It does work; what is happening is that the nextInt() method only reads digits from the input stream; if you have typed, say "123<enter>" the characters 1, 2 and 3 are read (giving a result of 123) but the <enter> character is left in the input buffer. When a String needs to be read the method sees the <enter> character and assumes an empty String.
Terefore you should read the rest of the line (containing that <enter> character) before you attempt to read another String.
This scenario also fails when you type everything on a single line, say "123 456 John Doe" but see for yourself ...
kind regards,
Jos
- 06-17-2010, 01:28 PM #6
nextInt() does NOT read past the newline char created by Enter. When you then do a nextLine() that reads and eats the newline char that followed where nextInt() left off.
What is returned in your case is an empty string.
To test this, using your code as shown above in post #1, enter the first name on the SAME line as the second number before pressing Enter.Last edited by Norm; 06-17-2010 at 05:34 PM. Reason: Correct wording: eats vs returns
- 06-17-2010, 01:29 PM #7
I guess I'm just a Copy cat. Too slow.
- 06-17-2010, 03:44 PM #8
Now look at this code then tell me
Output is:Java Code:import java.util.Scanner; public class aaa { public static void main ( String args[] ) { Scanner input = new Scanner( System.in ); String firstName, lastName; int num1, num2; System.out.print( "Enter first name: " ); firstName = input.nextLine(); System.out.print( "Enter last name: " ); lastName = input.nextLine(); System.out.printf( "Your full name is %s %s", firstName, lastName ); System.out.print( "\nEnter first number: " ); num1 = input.nextInt(); System.out.print( "Enter second number: " ); num2 = input.nextInt(); System.out.printf( "Sum is %dn", num1 + num2 ); } }
Enter first name: Cluster
Enter last name: Storm
Your full name is Cluster Storm
Enter first number: 1
Enter second number: 2
Sum is 3
I put this before the integer input and
Now this is doing fine and your suggestion become false here . . .
what problem with it if we use after the integer input ??
Please ponder upon my problem . . .
Thanks
- 06-17-2010, 03:57 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
You changed the problem: now you are reading lines first followed by reading to ints; in your OP you had it the other way around. Also (re)read the replies given before because they are all valid and nothing is 'false' in my or any other reply.
kind regards,
Jos
ps. what does this have to do with Swing?
- 06-17-2010, 04:10 PM #10
@JosAh
please clear my confusion which is that look at the previous code (#8) when i typed a string input like "Cluster<enter>" then what happen ?? why whole ( Cluster and <enter> ) consider as a string not Cluster ??
In swing there is different thing all input deal as a string, so there is no problem with it. Problem appear when it is in command line . . .
Thanks . . .
- 06-17-2010, 04:16 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 06-17-2010, 05:36 PM #12
With your new version of the program, change the how you respond to the question:
Enter first number:
by entering: 1 2 <press enter>
Giving the two numbers on the same line. This will pre-feed the next nextInt().
- 06-17-2010, 05:40 PM #13
Similar Threads
-
Problem with while loop and the scanner method
By hhh80p in forum New To JavaReplies: 2Last Post: 02-28-2010, 11:47 AM -
[Paremeters] in a method(Object expA, Object expB){}
By Unsub in forum New To JavaReplies: 2Last Post: 01-29-2010, 02:01 AM -
Why do I need to declare a new Scanner object here?
By Chase in forum New To JavaReplies: 3Last Post: 09-24-2008, 11:59 PM -
Regarding Scanner Object -- Cannot Resolve the Symbol
By pascal45 in forum New To JavaReplies: 1Last Post: 12-21-2007, 11:12 AM -
Using the scanner method
By silvia in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks