Hi,

I'm having trouble with...I don't know, either choosing the correct loop to use or maybe using the one I need but having it incorrectly placed. Right now, the program will give the "Invalid item number" message from my else statement if I enter an invalid value in the first prompt only and then close. I need it to do that on any prompt for the item number, not just the first one. I also noticed that it seems my switch statement and the calculations work for the first set of prompts but after that, no matter what item number I enter, it still seems to think that it's the value for the first case that I entered but the calculations still work based on the quantity I put in, it just still calculates it using the value from whatever case I entered in the first prompt. I may be wrong but, I'm thinking this also has to do with my loop, whether it be that I'm using the wrong kind or that it's just placed wrong. I don't think I can use a for loop because what I need is a sentinel-controlled loop that will stop when an invalid number (a value not 1-5) is entered into the prompt asking what item the user would like to order. I don't think the for loop allows that. I am still new to programming so I may be way off the mark here. Anyway, here's my code. Can anyone see where I might be messing up?

/**
      Program #3
      
      Function: Calculate order total using user input.
      
      Program by: Barbara Harris (March 31, 2010)
      
*/

import javax.swing.JOptionPane;
public class order
{
   public static void main(String[] args)
   {
   
   	String quantityString;
      int quantity;
		String productString;
      int product = 0;
      double total;
      double grandtotal = 0;
      int x;
      
      
      productString = JOptionPane.showInputDialog(null, "What product (1-5) would you like to order?", "First Item Number", JOptionPane.QUESTION_MESSAGE);
      product = Integer.parseInt(productString);
      
      x = product;
		
	if(x > 0 && x <= 5)
		{	  	
			while(x > 0 && x <=5)
   		{
		 		quantityString = JOptionPane.showInputDialog(null, "How many of that product would you like to order?", "Quantity", JOptionPane.QUESTION_MESSAGE);
         	quantity = Integer.parseInt(quantityString);

         	double prodprice = 0;
         	switch(product)
         	{
            	case 1:
            		prodprice = 2.00;
              		break;
         
            	case 2:
               	prodprice = 4.00;
               	break;
         
            	case 3: 
               	prodprice = 6.00;
              		break;
          
            	case 4:
               	prodprice = 5.00;
               	break;
         
            	case 5:
               	prodprice = 3.00;
               	break;
         
            	default:
               	JOptionPane.showMessageDialog(null, "Invalid product number.", "Error", JOptionPane.INFORMATION_MESSAGE);
     					break;
						       
				}  
			
      		total = quantity * prodprice;
         	grandtotal = grandtotal + total;
         
         	JOptionPane.showMessageDialog(null, "The total amount for this product is $" + total + ".", "Item Total", JOptionPane.INFORMATION_MESSAGE);
         	JOptionPane.showMessageDialog(null, "The total of your entire order so far is $" + grandtotal + ".", "Grand Total", JOptionPane.INFORMATION_MESSAGE);
         	JOptionPane.showInputDialog(null, "Please enter the number of the next item you would like to add to your order.", "Item Number", JOptionPane.INFORMATION_MESSAGE);
			}
		}
		else
		{
   		JOptionPane.showMessageDialog(null, "Invalid item number.", "Error", JOptionPane.ERROR_MESSAGE);
   	}
	}            
}

Looks like the problem is your not re-declaring a value for the variable product. In the first instancec, you set the dialog window to the variable productString, and then parse it into the product variable. However, at the end of the loop when you prompt the user, you aren't assigning it to anything, so the variable 'product' is remaining the same. Try to modify it like you did at the begining, so around line 71 it looks something like:

prodcutString = JOptionPane.showInputDialog(null, "Please enter the number of the next item you would like to add to your order.", "Item Number", JOptionPane.INFORMATION_MESSAGE);
	product = Integer.parseInt(productString);

Looks like the problem is your not re-declaring a value for the variable product. In the first instancec, you set the dialog window to the variable productString, and then parse it into the product variable. However, at the end of the loop when you prompt the user, you aren't assigning it to anything, so the variable 'product' is remaining the same. Try to modify it like you did at the begining, so around line 71 it looks something like:

prodcutString = JOptionPane.showInputDialog(null, "Please enter the number of the next item you would like to add to your order.", "Item Number", JOptionPane.INFORMATION_MESSAGE);
	product = Integer.parseInt(productString);

Good Lord!!!! I can't believe I overlooked that!!! I knew it must have been some small thing that I was just overlooking. Thank you SOOOO much for pointing that out!

I think my program is FINALLY doing what it's supposed to now!!! Thank you so much for your help! I really owe you one! Just in case someone else who needs help comes across this post, I'm pasting my WORKING code in this message.

/**
      Program #3
      
      Function: Calculate order total using user input.
      
      Program by: Barbara Harris (March 31, 2010)
      
*/

import javax.swing.JOptionPane;
public class order1
{
   public static void main(String[] args)
   {
   
   	String quantityString;
      int quantity;
		String productString;
      int product;
      double total;
      double grandtotal = 0;
      int x;
	
     

           
			
			productString = JOptionPane.showInputDialog(null, "What product (1-5) would you like to order?", "Item Number", JOptionPane.QUESTION_MESSAGE);
     		product = Integer.parseInt(productString);
			x = product;     
        
			 
      	while(x > 0 && x <=5)
   		{
						
      						
					double prodprice = 0;
         		switch(product)
         		{
            	case 1:
            		prodprice = 2.00;
              		break;
         
            	case 2:
               	prodprice = 4.00;
               	break;
         
            	case 3: 
               	prodprice = 6.00;
              		break;
          
            	case 4:
               	prodprice = 5.00;
               	break;
         
            	case 5:
               	prodprice = 3.00;
               	break;
         
            	default:
               	JOptionPane.showMessageDialog(null, "Invalid product number.", "Error", JOptionPane.ERROR_MESSAGE);
     					break;
         	}
				
				quantityString = JOptionPane.showInputDialog(null, "How many of that product would you like to order?", "Quantity", JOptionPane.QUESTION_MESSAGE);
         	quantity = Integer.parseInt(quantityString);

				
				total = quantity * prodprice;
         	grandtotal = grandtotal + total;

				
         	JOptionPane.showMessageDialog(null, quantity + " of product #" + product + " costs $" + total + ".", "Item Total", JOptionPane.INFORMATION_MESSAGE);
         	JOptionPane.showMessageDialog(null, "The total of your entire order so far is $" + grandtotal + ".", "Grand Total", JOptionPane.INFORMATION_MESSAGE);
      		productString = JOptionPane.showInputDialog(null, "Please enter the number of the next item that you'd like to order.", "Item Number", JOptionPane.QUESTION_MESSAGE);
				product = Integer.parseInt(productString);
				x = product;	
			}
			
			JOptionPane.showMessageDialog(null, "Invalid item number!", "Error", JOptionPane.ERROR_MESSAGE);
			JOptionPane.showMessageDialog(null, "Your order total is $" + grandtotal + ".", "Order Complete", JOptionPane.INFORMATION_MESSAGE);
			
		}
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.