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

Thread: java.lang.numberformatexception error

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java.lang.numberformatexception error

    Hi, im new to java, and im having a problem.
    i have written a program that allows the user to select a choice 1-5 from a menu. then the desired info is displayed. if any other numbers are entered and error message appears. but i would like this error message to appear also if the user enters a symbol or anything that is not a number 1-5. how do i do this?

    whenever i enter a symbol i get a java.lang.numberformatexception error message.i know it has to do with the highlighted area.

    below is part of my code:

    do
    		{
    			do
    			{
    				//Runs Public Static void display_menu()
    				display_menu();
    				[COLOR="Red"]choice = Integer.valueOf(in.readLine()).intValue();[/COLOR]
     
    				if ((choice < 1) || (choice > 5))
    				{
    					System.out.println("Error: invalid choice, please try again");
    				}
    			} while ((choice < 1) || (choice > 5));
    			if (choice != 5)
    			{
    				switch (choice)
    				{
    					case 1: 
    						//Runs Public Static void Village()
    						village();
    						break;
    					case 2:
    						//Creates Output Text
    						System.out.println("----------------------------");
    						System.out.println("---Milk Round Information---");
    						System.out.println("----------------------------");
    						System.out.println("Distributer's Name: Ben Siddall");
    						System.out.println("Number of Pints Needed for Sale each day: " + milkman.pints_to_be_available_daily());
    						System.out.println("Total Revenue for the Milk Round each week: "  + myFormat.format(milkman.milk_round_revenue())+" Pounds");
    						break;
    					case 3:
    						//Creates Output ~Text
    						System.out.println("-------------------------");
    						System.out.println("---Farmer Information---");
    						System.out.println("-------------------------");
    						System.out.println("Farmer's Name: Caleb Green");
    						System.out.println("Number of Pints needed each day: " + milkman.pints_to_be_available_daily());
    						System.out.println("Number of Cows required to Produce milk: " + farmer.cows_required(milkman.pints_to_be_available_daily()));
    						System.out.println("Total Revenue for the Farmer each week: " + myFormat.format(milkman.farmers_revenue()) +" Pounds");
    						System.out.println("Predicted Annual Milk Production: " + farmer.expected_pints_year() + " Pints");
     
    						break;
    					case 4:
    						//creates output text
    						System.out.println("-------------------------");
    						System.out.println("---Revenue Information---");
    						System.out.println("-------------------------");
    						System.out.println("Milk Round Revenue: " + myFormat.format(milkman.milk_round_revenue()) + " Pounds");
    						System.out.println("Farmers Revenue: " + myFormat.format(milkman.farmers_revenue()) + " Pounds");
    						System.out.println("Farmers percentage of the milk round revenue: " +  myFormat.format(milkman.farmers_percentage_revenue()) + " % ");
    						System.out.println("Predicted Annual Revenue for the Milk Round: " + myFormat.format(milkman.annual_milk_revenue())  + " Pounds");
     
    						break;
    					default:
    						break;
    				}
    			}
    		} while (choice != 5);
    Last edited by Json; February 18th, 2010 at 09:33 AM. Reason: Please use code tags.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: java.lang.numberformatexception error

    Hello,

    The problem you have is that the input you receive in in.readLine() is not a number and when that happens the JVM throws a NumberFormatException.

    There is also a somewhat shorter way for you to get the integer value from the string as a primitive.

    choice = Integer.parseInt(in.readLine());

    To solve your problem and print an error you can do this.

    try {
        choice = Integer.parseInt(in.readLine());
    } catch (Throwable throwable) {
        System.out.println("Error: your choice needs to be a number, please try again");
    }

    // Json

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.numberformatexception error

    where do i put the try catch code? do i replace part of mine with it, or place within my code????

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: java.lang.numberformatexception error

    See the PM I sent you.

    // Json

Similar Threads

  1. java.lang.UnsatisfiedLinkError
    By H P in forum Java Native Interface
    Replies: 3
    Last Post: January 28th, 2010, 05:04 AM
  2. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM
  3. java.lang.NullPointerException - Help
    By mds1256 in forum Exceptions
    Replies: 5
    Last Post: November 30th, 2009, 06:31 PM
  4. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  5. Getting "AWT-EventQueue-0" java.lang.NullPointerException error
    By tryingtoJava in forum AWT / Java Swing
    Replies: 9
    Last Post: September 21st, 2009, 10:46 PM

Tags for this Thread