Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Scanner object not allowing input

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Scanner object not allowing input

    Hello friends,


    Started learning Java last week. I have experience with C++ and the similarities between both are making it easier for me to understand Java.

    A quick subquestion: Is the scanner class and scanner object the basic equivalent for console input (cin >> ) for C++? And is the + used as the insertion operator (>> and <<) when printing variables/etc? Please forgive my newbiness. I just want to catch and throw out any incorrect assumptions early.

    Anyways, here is my code. My problem is that if I ask the user to input a string and then an integer, I get the expected output. If, however, I ask the user to input an int and then a string, after inputting the int, it terminates the program, not allowing me to input a string value in name.

    import java.util.Scanner; //Needed for console input object
     
    public class FirstClass {
    public static void main (String [] args)
    	{
     
    		//User input with different data types
    		Scanner scan = new Scanner(System.in);
     
    		System.out.println("Enter your name: ");
    		String name = scan.nextLine();
     
    		System.out.println("Enter your age: ");
    		int age = scan.nextInt();
     
    		System.out.println("Your name is " + name + " and you are " + age + " years old.\n");
    	}	
    }
     
    //OUTPUT: Your name is John Smith and you are 25 years old.





    import java.util.Scanner; //Needed for console input object
     
    public class FirstClass {
    public static void main (String [] args)
    	{
    		//User input with different data types
    		Scanner scan = new Scanner(System.in);
     
                    System.out.println("Enter your age: ");
    		int age = scan.nextInt();
     
    		System.out.println("Enter your name: ");
    		String name = scan.nextLine();
     
    		System.out.println("Your name is " + name + " and you are " + age + " years old.\n");
    	}	
    }
    //OUTPUT: Your name is and you are 25 years old.

    Thanks in advance.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Scanner object not allowing input

    There is a problem with the way the Scanner class works. When using a method other than the nextLine method, the line-end character from pressing Enter is left in the buffer. The next call to nextLine will return that line-end character which results in returning an empty String.
    The solution is to call nextLine once after nextInt to read that line-end character. The next call to nextLine will retrieve what is entered correctly. Do a search on the internet for more information.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    jgialis (September 16th, 2019)

  4. #3
    Junior Member
    Join Date
    Sep 2019
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Scanner object not allowing input

    Thank you! I appreciate your time in answering my question Have a great day.

Similar Threads

  1. File input using Scanner help.
    By AaronHarris in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 4th, 2013, 07:15 AM
  2. Scanner skipping input
    By hblakek in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 21st, 2012, 01:44 PM
  3. Dialog box not allowing user input?
    By michaelgilbert in forum AWT / Java Swing
    Replies: 13
    Last Post: November 1st, 2011, 05:22 PM
  4. [SOLVED] Question About Using Scanner Object Twice
    By derekeverett in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: July 16th, 2011, 10:36 AM
  5. Allowing user to input variable.
    By That1guy in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 11:30 PM

Tags for this Thread