Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble with Exception Handling

 
john leeman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void totals(int packages)
{
{
String sun = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Sunday");
try
{
sundays = Integer.parseInt(sun);
if (sundays >50)
JOptionPane.showMessageDialog(null, "Sorry, Maximum Available for Rental is 50.");
}
catch(NumberFormatException e)
{
System.out.println(e.toString());
JOptionPane.showMessageDialog(null, "Please Enter A Valid Rental Amount");
}
}


String mon = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Monday");
monday = Integer.parseInt(mon);

String tues = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Tuesday");
tuesday = Integer.parseInt(tues);

String wed = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Wednesday");
wednesday = Integer.parseInt(wed);

String thurs = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Thursday");
thursday = Integer.parseInt(thurs);

String fri = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Friday");
friday = Integer.parseInt(fri);

String sat = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Saturday");
saturday = Integer.parseInt(sat);
}


I am having problems with my Exception Handling.

I need to limit the input to be less than 50.
If the user enters anything over 50, it will display:Sorry, Maximum Available for Rental is 50.

If the user enters any characters it will display:Please Enter A Valid Rental Amount

I want it to keep repeating the question until the user enters the correct input.

I am trying to use the Exception Handling for each JOptionPane.showInputDialog.

Not sure what I'm doing wrong here. It gives me the wrong input messages for over 50 and characters, but it doesn't let me re-enter the information.

What am I doing wrong?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
It looks like you need a loop. Currently, you only get prompted once. As you stated, you want to get prompted until the user enters something valid.
 
john leeman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
{
codes = 0;
while(sunday > 50)
{
String sun = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Sunday");
try
{
sunday = Integer.parseInt(sun);
if (sunday >50)
JOptionPane.showMessageDialog(null, "Sorry, Maximum Available for Rental is 50.");
}
catch(NumberFormatException e)
{
System.out.println(e.toString());
JOptionPane.showMessageDialog(null, "Please Enter A Valid Rental Amount");
sunday = 0;
}
}


String mon = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Monday (max. 50).");
monday = Integer.parseInt(mon);

String tues = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Tuesday (max. 50).");
tuesday = Integer.parseInt(tues);

String wed = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Wednesday (max. 50).");
wednesday = Integer.parseInt(wed);

String thurs = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Thursday (max. 50).");
thursday = Integer.parseInt(thurs);

String fri = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Friday (max. 50).");
friday = Integer.parseInt(fri);

String sat = JOptionPane.showInputDialog("Package 0 (Living Furniture) \n Enter Item Total for Saturday (max. 50).");
saturday = Integer.parseInt(sat);
}


I'm not sure what I'm doing wrong, it skips asking Sundays input altogether...

it goes straight to mondays.
[ June 02, 2005: Message edited by: john leeman ]
 
john leeman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
never mind, i just figured it out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic