Results 1 to 6 of 6
  1. #1
    bdearen is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default New to Java Looking for help

    Hey guys I have an assignment I am doing for school. I just started it but am havng a problem. Basically I need the variable unitName to be able to take a string like "adobe photoshop" 2 words or a word space word space word. So I swithced this line to
    Java Code:
    unitName = input.nextLine();
    before this I was using input.next(); I was able to do some research and learn that nextLine will allow white spaces ect.. However after the program runs once it gives an error when it loops and starts the 2nd time. If i only enter one word for the string unitName it will loop and run correctly. Again this project is not complete just showing you guys what I have here so far. I think the error is at the bottom
    Java Code:
    while(!unitName.equalsIgnoreCase("stop"));
    right here.. but what do I change this to so it will run and loop when unitName = 2 words. Thanks Also the program should loop until stop is entered as the unitName.

    Java Code:
    import java.util.Scanner;
    
    
    public class InventoryControl 
    {
    	
    	public static void main(String[] args) 
    	{
    		Scanner input = new Scanner(System.in); //create object
    		
    		String unitName=""; // Declare Variables
    		int unitNumber=0; 
    		int unitStock=0;
    		double unitPrice=0;
    		double unitInventory=0; //Inventory = units in stock * unit price
    		
    	do{
    		
    		System.out.print("\nWelcome to the inventory control program. Enter stop to quit the program.  \nPlease enter the name of the product");
    		unitName = input.nextLine();
    		
    		if(unitName.equalsIgnoreCase("stop")) //unitName variable check
    		
    		{
    		
    			System.out.println("Thank you for using the inventory control program!");
    			System.exit(0);
    			
    		} //end if unitName variable check
    		
    		else
    		
    		System.out.print("Please enter a unit number");
    		unitNumber = input.nextInt();
    		
    			while (unitNumber <= 0) //checks unitNumber >= 0
    				{
    					System.out.println("Please enter a positive number");
    					unitNumber = input.nextInt();
    				}
    		
    		System.out.print("Please enter unit stock (how many do we have?)");
    		unitStock = input.nextInt();
    		
    			while (unitStock <= 0) //checks unitStock >= 0
    				{
    					System.out.println("Please enter a positive number");
    					unitStock = input.nextInt();
    				}
    		
    		System.out.print("Please enter unit price (how much does it cost?)");
    		unitPrice = input.nextDouble();
    		
    			while (unitPrice <= 0) //checks unitPrice >= 0
    				{
    					System.out.println("Please enter a positive number");
    					unitPrice = input.nextInt();
    				}
    		
    		} //end do loop
    		
    		while(!unitName.equalsIgnoreCase("stop"));
    
    	} //end main
    
    } //end class InventoryControl

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    This is a common problem learners run into when using Scanner. The nextInt() method leaves a trailing newline ('\n') in the input stream. As long as you're only doing nextInt() calls, you don't notice this, as nextInt() will treat a leading newline as white space and scan past it to the int you want to read. But when you do nextLine(), it reads only the newline, and you get an empty String in your variable, and what you entered stays in the input stream, where nextInt() tries to read it and gives an error.

    There are two ways to work around this:

    1) Use nextLine() all the time with a String variable, then do an Integer.parseInt() call when you want to convert the String to an int.
    Java Code:
            String myString = scanner.nextLine();
            int myInt = Integer.parseInt(myString);
    2) Follow every nextInt() call with a nextLine() call. (Technically, it doesn't really have to be every nextInt() call, as discussed above, but this is the safest practice.)
    Java Code:
            int myInt = scanner.nextInt();
            scanner.nextLine(); // throw away the newline character
    I prefer the first approach, but either works. Note that the second approach doesn't work if you're trying to scan more than one int from a single line of input.

    -Gary-
    Last edited by gcalvin; 02-25-2011 at 06:59 PM.

  3. #3
    bdearen is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Java Code:
     String myString = scanner.nextLine();
            int myInt = Integer.parseInt(myString);
    So for this example all of my Int would be Strings? And I would convert them for each line?

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by bdearen View Post
    Java Code:
     String myString = scanner.nextLine();
            int myInt = Integer.parseInt(myString);
    So for this example all of my Int would be Strings? And I would convert them for each line?
    Yes, that's how I would do it.

    -Gary-

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Quote Originally Posted by Java.Instructor View Post
    Please mail me
    That's not what a forum is for. Please find somewhere else to harvest email addresses.

    db

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Posting Permissions

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