Results 1 to 7 of 7
  1. #1
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

    Default Consol input [Problem]

    Hi i am new to this forums and i liked it too much as i saw other people problems and their solutions given by moderators or senior members which is very helpful.........
    Ok yesterday i was doing a lab task i got a problem with nextLine() and next()..

    Ok problems is
    Java Code:
    Scanner input=new Scanner(System.in);
    String S1=input.nextLine();
    String S2=input.nextLine();
    In above code my program terminates and i dont get desired results... So i have to create multiple Scanner objects like:
    Java Code:
    Scanner input=new Scanner(System.in);
    String S1=input.nextLine();
    Scanner inp=new Scanner(System.in);
    String S2=input.nextLine();
    Now my program runs correctly........... but when i use next() i get no problem and program does not terminates....
    Java Code:
    Scanner input=new Scanner(System.in);
    String S1=input.next();
    String S2=input.next();
    here my program does not terminates........

    My question is that why it is behaving like this ... i know that next() only picks up one string while nextLine() picks up whole line of multiple strings ...... but above mentioned problem is unuderstandable for me........

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Consol input [Problem]

    Multiple Scanners is pointless.
    There's something else going on in your code.

    What input are you trying to read?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

    Default Re: Consol input [Problem]

    Java Code:
    public static void main(String args[])
    {
    Invoice obj=new Invoice();
    int option;
    int x;
    String s;
    Scanner input=new Scanner(System.in);
    do
    {
    System.out.println("Welcome to Java Program. \nPress 1 to Set values.\nPress 2 to view values.\nPress 0 to terminate");
    option=input.nextInt();
    			if(option==1)
    			{
    				System.out.println("Press 1 to set Product No.\nPress 2 to set Product Description.\nPress 3 to Set quantity.\nPress 4 to set Price");
    				option=input.nextInt();
    				if (option==1)
    				{
    				System.out.println("Please enter the product number.");
    				Scanner in=new Scanner(System.in);
    				s=in.nextLine();
    				obj.set_p_no(s);
    				}
    				else if(option==2)
    				{
    				Scanner inp=new Scanner(System.in);
    				System.out.println("Please enter the product Description.");
    				s=inp.nextLine();
    				obj.set_p_d(s);
    				}
    				else if (option==3)
    				{
    				Scanner inpu=new Scanner(System.in);
    				System.out.println("Please enter the product quantity.");
    				x=inpu.nextInt();
    				obj.set_q(x);
    				}
    				else if (option ==4)
    				{
    				Scanner i=new Scanner(System.in);
    				System.out.println("Please enter the product price.");
    				x=i.nextInt();
    				obj.set_price(x);
    				}
    				else 
    				System.out.println("Sorry Invalid input.Try AGAIN!!!.");
    			}
    				else if(option==2)
    				{
    					System.out.println("\t\t\tThe output as whole is as follows:");
    					System.out.println("\t\t\tProduct Number:   \t" + obj.get_p_no());
    					System.out.println("\t\t\tProduct Description:    " + obj.get_p_d());
    					System.out.println("\t\t\tProduct quantity:  \t" + obj.get_q());
    					System.out.println("\t\t\tProduct Price:   \t" + obj.get_price());
    					System.out.println("\t\t\tInvoice Amount:  \t" + obj.getInvoiceAmount());
    				}
    				else if(option==0)
    				{
    				break;
    				}
    				else
    						System.out.print("Sorry Invalid value.Try AGAIN!!!");
    }while(option!=0);
    						}

    This is the code and now it is working fine with multiple scanner objects...

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Consol input [Problem]

    It's the different nature of the nextXXX() methods (including next()) and the nextLine() method.
    The former leave the new line character(s) in the buffer, which means if you do:
    Java Code:
    nextInt();
    nextLine();
    the nextLine() calls simply returns that (empty) line that has been left in the buffer.
    If you need the whole line then you need to make an extra nextLine() call to clear that left over newline in the buffer.

    No need for multiple Scanners.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

    Default Re: Consol input [Problem]

    This is my problem i am saying when i remove multiple objects my program terminates incorrectly.......
    Java Code:
    if (option==1)
                    {
                    System.out.println("Please enter the product number.");
                    Scanner in=new Scanner(System.in);
                    s=in.nextLine();
                    obj.set_p_no(s);
                    }
    Program do not takes inpur and just show the print message and terminates if i remove multiple scaners

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Consol input [Problem]

    And I just explained to you why it is doing that.
    You have two options...either make an extra call to nextLine() after any particular nextInt() call, or change the delimiter to a newline, and not use the default whitespace, then you can use next() instead.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

Similar Threads

  1. Problem with detecting input
    By ConnorSG1 in forum New To Java
    Replies: 5
    Last Post: 11-20-2012, 06:23 PM
  2. Problem reading input from cmd.
    By peachyco in forum New To Java
    Replies: 9
    Last Post: 10-14-2010, 05:59 AM
  3. problem !! input on GUI to a file
    By javanew in forum AWT / Swing
    Replies: 1
    Last Post: 09-21-2010, 01:09 AM
  4. Input problem
    By Sotsiak in forum New To Java
    Replies: 1
    Last Post: 06-04-2010, 01:07 PM
  5. Detect keypresses without consol or GUI
    By somanayr in forum New To Java
    Replies: 7
    Last Post: 05-16-2010, 05:56 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
  •