Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default Problem Of Scanner Object with its method nextLine()

    Hy . . .

    Here is the code . . .

    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 );
    		
    		
    	}
    }
    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 ??
    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 . . .:)

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    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:

  3. #3
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Hi,
    Might be cursor is not returing to the next step after taking integer.
    Brother this is not working . . . Any other idea ??

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    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:

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Cluster Storm View Post
    Brother this is not working . . . Any other idea ??
    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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    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 06:34 PM. Reason: Correct wording: eats vs returns

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    I guess I'm just a Copy cat. Too slow.

  8. #8
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    It does work; kind regards,
    Jos
    Now look at this code then tell me
    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 );
    
    
    		
    		
    	}
    }
    Output is:

    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

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    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?

  10. #10
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    @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 . . .

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Cluster Storm View Post
    @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 . . .
    Read the API documentation; don't start coding anything before reading it; according to that documentation the readLine() method strips the trailing <enter> character but does read it so your String ends up to be "Cluster".

    kind regards,

    Jos

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    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().

  13. #13
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

Similar Threads

  1. Problem with while loop and the scanner method
    By hhh80p in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 12:47 PM
  2. Replies: 2
    Last Post: 01-29-2010, 03:01 AM
  3. Replies: 3
    Last Post: 09-25-2008, 12:59 AM
  4. Replies: 1
    Last Post: 12-21-2007, 12:12 PM
  5. Using the scanner method
    By silvia in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •