• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sentinal controlled looop

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a simple app that is required to have a switch structure, used with a sentinal. It compiles fine, but I am only able to as far as my second JOption pane and enter data. After that the program stops. I think the the problem is somewhere in my use of a while statement with a switch. See below:

public class MailOrder {
public static void main( String args[] )
{


int product, // first number to add
quantitySold; // second number to add
double total, // total of all products
allValue;

String productNumber; // product sold

// read in productNumber from user
productNumber =
JOptionPane.showInputDialog(
"Enter number for product sold 1 through 5\nEnter -1 to Quit\n");
String quantity; // number of products sold
// read in quantity from user
quantity =
JOptionPane.showInputDialog(
"Enter quantity of product sold");
// convert numbers from type String to type integer
product = Integer.parseInt( productNumber );
quantitySold = Integer.parseInt( quantity );

total = 0;
allValue = 0;


while ( product != -1 ) {
// add Retail Value to total
allValue = total + allValue;


switch(product) {
case 1:
total = total + (quantitySold * 2.98);
break;
case 2:
total = total + (quantitySold * 4.50);
break;
case 3:
total = total + (quantitySold * 9.98);
break;
case 4:
total = total + (quantitySold * 4.49);
break;
case 5:
total = total + (quantitySold * 6.87);
default:
JOptionPane.showMessageDialog(
null, "Invalid value entered" );

// read in productNumber from user
productNumber =
JOptionPane.showInputDialog(
"Enter number for product sold 1 through5\nEnter -1 to Quit\n");

// read in quantity from user
quantity =
JOptionPane.showInputDialog(
"Enter quantity of product sold");
// convert numbers from type String to type integer
product = Integer.parseInt( productNumber );
quantitySold = Integer.parseInt( quantity );
}

// termination phase
DecimalFormat precisionTwo = new DecimalFormat( "0.00" );
if ( product == -1 ) {
// add Retail Value to total
allValue = total + allValue;
// display value of all products sold
JOptionPane.showMessageDialog( null,
"Total Retail Value of all products sold Last Week" + precisionTwo.format(allValue),
"Value of all Products",
JOptionPane.INFORMATION_MESSAGE );
}
System.exit( 0 ); //terminate the program
}
}
}
/****************************END*****************************/

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When posting code here, please be sure to indent the code properly and use the [ code ] and [ /code ] tags (without the spaces) to preserve that indentation when you post. You can read about these tags here.
Also, the main method here is far too long for my taste - there are too many things happening here, and it's hard to understand. It would really help you to see exactly what your code is doing if you were to move a lot of this code into separate methods, so that each method is an average of 10 lines or less. I know this seems like extra work right now, but it really will pay off in the long run to help everyone understand your code - especially you. A good start would be to at least take out most of the stuff inside the while statement, and put it in a separate method.
Anyway, as for your specific problem - I think you need to look carefully at just where your switch statement ends. The "default:" section includes not just the "Invalid value" message, but also the JOptionPanes to get more user data. So right now, those JOptionPanes are only being executed when product is outside the range 1-5. If that's not what you want, you need to move them outside the switch statement. If you had put the entire switch statement in a separate method, this would be much more obvious.
Good luck.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for all the advise, this was my first post to the site, didn't know about the code posting rules.
Your suggestions did help me clean up the app and get it working!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic