Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Trouble with Exception Handling

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author & internet detective
Posts: 40912
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
never mind, i just figured it out.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic