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 2 of 2

Thread: Skipping Input Problem

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Skipping Input Problem

    Hi im new to this forum and to java and i just had a few simple questions i've been trying to solve.

    Basicly i'm just using my scanner (Input) to fill up some different types of variables
    		System.out.print("Product Code: ");				
    		Code = Input.nextLine();		   			
     
    		System.out.println("Product Discription: ");	
    		Discription = Input.nextLine();		   		
     
    		System.out.println("Price: $");					
    		Price = Input.nextDouble();		   				
     
    		System.out.println("Quantitiy: ");				
    		Quantity = Input.nextShort();		   			
     
    		System.out.println("Sales Rep: ");			 	
    		Name = Input.nextLine();		//<--- this is where the problem lies

    Once I get to the sales rep input it skips in entirely and just leaves it blank.

    Thanx in advance
    -Matty Alan


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Skipping Input Problem

    Hello Matty Alan,

    Welcome to the Java Programming Forums.

    I tested this and can see that it does indeed skip the last Name = Input.nextLine();
    Upon further testing and reading about the Scanner class, the issue seems to be down to nextLine(); method.

    Scanner (Java Platform SE 6)

    Quote Originally Posted by Oracle
    Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

    Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.
    If you update to:

    		System.out.print("Product Code: ");				
    		Code = Input.next();		   			
     
    		System.out.println("Product Discription: ");	
    		Discription = Input.next();		   		
     
    		System.out.println("Price: $");					
    		Price = Input.nextDouble();		   				
     
    		System.out.println("Quantitiy: ");				
    		Quantity = Input.nextShort();		   			
     
    		System.out.println("Sales Rep: ");			 	
    		Name = Input.next();

    Then it works fine.

    But it doesn't solve the issue for the "Discription" variable.
    I'm sure the description will contain more than one word and Input.next(); will throw a java.util.InputMismatchException if we try this.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. [SOLVED] if else skipping
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2010, 09:04 PM
  2. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 05:44 AM
  3. Problem with Console Input from Clipboard
    By Nilhanth in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 1st, 2010, 06:47 PM
  4. need help prog skipping method
    By Pulse_Irl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 11th, 2010, 08:57 AM
  5. Java error while using BufferedReader class to read a .txt document
    By Jchang504 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: February 4th, 2009, 07:55 PM