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:
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);
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.
Code :
choice = Integer.parseInt(in.readLine());
To solve your problem and print an error you can do this.
Code :
try {
choice = Integer.parseInt(in.readLine());
} catch (Throwable throwable) {
System.out.println("Error: your choice needs to be a number, please try again");
}
// Json
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????
Re: java.lang.numberformatexception error
See the PM I sent you.
// Json