Link to home
Start Free TrialLog in
Avatar of maqskywalker
maqskywalker

asked on

java try and catch block

hi experts,

I'm practicing Java fundamentals.
I'm using Eclipse.

I have a program called Test1.java.

This is my code.

import javax.swing.JOptionPane;

public class Test1 {

	public static void main(String[] args) {

		String number1, number2;
		double fnum, snum;
                int x = 1;

        do{
        	
        	try{						
        		// read in first number from user as a string
        		number1 = JOptionPane.showInputDialog( "Enter first number" );
        		// convert numbers from type String to type double
        		fnum = Double.parseDouble( number1 );
        		
				// read in second number from user as a string
				number2 = JOptionPane.showInputDialog( "Enter second number" );		
				snum = Double.parseDouble( number2 );
		
				// create new class instance
				Test1 invokingTest1 = new Test1();  		
				
				// display the results
				JOptionPane.showMessageDialog( null, "The sum of your two numbers is " + invokingTest1.addNumbers(fnum, snum), "Results", JOptionPane.PLAIN_MESSAGE );
		
				JOptionPane.showMessageDialog( null, "The difference of your two numbers is " + invokingTest1.subtractNumbers(fnum, snum), "Results", JOptionPane.PLAIN_MESSAGE );
				
				JOptionPane.showMessageDialog( null, "The product of your two numbers is " + invokingTest1.multiplyNumbers(fnum, snum), "Results", JOptionPane.PLAIN_MESSAGE );
				
				JOptionPane.showMessageDialog( null, "The quotient of your two numbers is " + invokingTest1.divideNumbers(fnum, snum), "Results", JOptionPane.PLAIN_MESSAGE );								
				
				// the variable x is assigned a value of 2
		        x=2;
        	}
        	catch(Exception e){
        		JOptionPane.showMessageDialog( null, "Sorry! You can only enter numbers. Please try again.", "Results", JOptionPane.PLAIN_MESSAGE );
        	}
        }while(x==1);	


	}
	
	public double addNumbers(double fnum, double snum) {		
		return fnum + snum;		
	}

	public double subtractNumbers(double fnum, double snum) {
		return fnum - snum;		
	}
		
	public double multiplyNumbers(double fnum, double snum) {
		return fnum * snum;		
	}	
	

	public double divideNumbers(double fnum, double snum) {
		return fnum / snum;		
	}

}

Open in new window



This program launches a window that asks a user for a number.
Then another window launches asking for a second number.
Then once the two numbers are entered the user is giving the sum, difference, product, quotient of those numbers in a window.
I'm practicing using the try and catch block statements.

So if a user enters tries to enter letters in either of the two number windows then they are told to try again.
So my problem is if a user enters a letter in the second number window, the please try again messages shows up after that.
But then after that, they have to start from the beginning again.

How do I modify my code so that if a user enters a letter for their second window, after the please try again window, it starts from the second window, instead of having to start from the first number window?
Avatar of unknown_routine
unknown_routine
Flag of United States of America image

you need to separate the windows logic, so You need 2 separate try catch blocks.

one for
                              
                    // read in first number from user as a string
                    number1 = JOptionPane.showInputDialog( "Enter first number" );
                    // convert numbers from type String to type double
                    fnum = Double.parseDouble( number1 );
                    
other for

// read in second number from user as a string
                        number2 = JOptionPane.showInputDialog( "Enter second number" );            
                        snum = Double.parseDouble( number2 );


So if an yone these fail you can show the dialog again.

Also get rid of while loop, it only complicate your code and you don not need it here.
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial