aspose file tools*
The moose likes Beginning Java and the fly likes Trouble with Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with Exception Handling" Watch "Trouble with Exception Handling" New topic
Author

Trouble with Exception Handling

john leeman
Greenhorn

Joined: Jun 02, 2005
Posts: 10
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

Joined: May 26, 2003
Posts: 30913
    
158

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
john leeman
Greenhorn

Joined: Jun 02, 2005
Posts: 10
{
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

Joined: Jun 02, 2005
Posts: 10
never mind, i just figured it out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with Exception Handling