import javax.swing.*;
import java.text.*;

public class Postage3{
	public static void main(String args[]){
		double weight, cost, count;
		int choice, choice2;
		
		DecimalFormat df = new DecimalFormat ("0.00");
		
		choice2 = 1;
		
		while(choice2 == 1){
		
		weight = Double.parseDouble(JOptionPane.showInputDialog("Enter weight of postage according to ounces"));
		
		if(weight <= 1){
			cost = weight * 0.30;
		}
		else{
			count = (weight - 1) / 0.5;
			cost = (1 * 0.30) + (count * 0.11);
		}
	
		choice = Integer.parseInt(JOptionPane.showInputDialog("Special Delivery?" +"\n"
			+"Type [1] for Yes [0] for No"));
					
		if(choice == 1)
		JOptionPane.showMessageDialog(null, "Weight of Letter: " +weight +"\n"
				+"Postage cost: " +df.format(cost) +"\n"
					+"Special Delivery is 5.00" +"\n"
						+"Total Cost: " +df.format(cost+5));
		else if(choice == 0)
		JOptionPane.showMessageDialog(null, "Weight of Letter: " +weight +"\n"
				+"Postage cost: " +df.format(cost) +"\n"
					+"Total Cost: " +df.format(cost));
		else
		JOptionPane.showMessageDialog(null, "Please input only 1 or 0");
		
//I want it to restart here after displaying error

		choice2 = Integer.parseInt(JOptionPane.showInputDialog("Do you want another transaction?" +
			"\nPress [1] for YES [0] for NO"));
		if(choice2 == 0)
			JOptionPane.showMessageDialog(null, "Thank You. Have a nice day.");
		
		else if(choice2 != 1 && choice2 != 0)
			JOptionPane.showMessageDialog(null, "ERROR Please Input [1] or [0] only");
		}
		
	System.exit(0);
	}
}

I want my program to restart to choice2 input question after displaying the error dialog box.

Recommended Answers

All 9 Replies

I want it to restart after displaying the error dialog box.. please help.

Well, lets look at this. Your while condition is

while (choice2 == 1)

So, in order for your while loop to "continue", choice2 needs to have what value?

Now, where are you "showing" your error messages? In an if statement block, right? So, what do you think you might add to that block to ensure that condition in your while loop expression is met?

choice2 = Integer.parseInt(JOptionPane.showInputDialog("Do you want another transaction?" +
            "\nPress [1] for YES [0] for NO"));
        if(choice2 == 0)
            JOptionPane.showMessageDialog(null, "Thank You. Have a nice day.");
        
        else if(choice2 != 1 && choice2 != 0)
            JOptionPane.showMessageDialog(null, "ERROR Please Input [1] or [0] only");
//After this error dialog box I want it to ask if the user wants to have another transaction..

If the user inputs 1 the whole program restarts coz of this

while(choice2 == 1){

Okay? I know that. I am asking you that when the user does not input 1 then what do you need to do to achieve the same effect, and where do you need to do it?

I honestly dont know.. All I've done is when the user inputs 0 it ends the program with a thank you. Else if its neither 1 nor 0 it shows an error dialog box. Can you help me rewrite my program so that it loops back to the "Do you want to have another transaction?" question after displaying the "Please input [1] or [0] only error?.. plss

Edit: Nevermind. I misunderstood you a bit. All I can say there is to place those few lines (the asking of the question and the if statements) into a "while(true)" loop and "break" from the loop when a valid answer is encountered.

It just restarts the whole program. Can you make it display only the

choice2 = Integer.parseInt(JOptionPane.showInputDialog("Do you want another transaction?" +
            "\nPress [1] for YES [0] for NO"));

message only?? plss

I have changed that post. Give the new advice a try.

import javax.swing.*;
import java.text.*;

public class Postage3{
    public static void main(String args[]){
        double weight, cost, count;
        int choice, choice2;
        
        DecimalFormat df = new DecimalFormat ("0.00");
        
        choice2 = 1;
        
        while(choice2 == 1){
        
        weight = Double.parseDouble(JOptionPane.showInputDialog("Enter weight of postage according to ounces"));
        
        if(weight <= 1){
            cost = weight * 0.30;
        }
        else{
            count = (weight - 1) / 0.5;
            cost = (1 * 0.30) + (count * 0.11);
        }
    
        choice = Integer.parseInt(JOptionPane.showInputDialog("Special Delivery?" +"\n"
            +"Type [1] for Yes [0] for No"));
                    
        if(choice == 1){
        JOptionPane.showMessageDialog(null, "Weight of Letter: " +weight +"\n"
                +"Postage cost: " +df.format(cost) +"\n"
                    +"Special Delivery is 5.00" +"\n"
                        +"Total Cost: " +df.format(cost+5));
                        choice2 = 2;}
        else if(choice == 0){
        JOptionPane.showMessageDialog(null, "Weight of Letter: " +weight +"\n"
                +"Postage cost: " +df.format(cost) +"\n"
                    +"Total Cost: " +df.format(cost));
                    choice2 = 2;}
        else
        JOptionPane.showMessageDialog(null, "Please input only 1 or 0");
    
        while(choice2 == 2){
        choice2 = Integer.parseInt(JOptionPane.showInputDialog("Do you want another transaction?" +
            "\nPress [1] for YES [0] for NO"));
        if(choice2 == 0)
            JOptionPane.showMessageDialog(null, "Thank You. Have a nice day.");
        
        else if(choice2 != 1 && choice2 != 0){
            JOptionPane.showMessageDialog(null, "ERROR Please Input [1] or [0] only");
            choice2 = 2;
            }
        }
        }
        System.exit(0);
    }
}

I finally figured it out ^_^

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.