Solved

java try and catch block

Posted on 2014-03-27
2
388 Views
Last Modified: 2014-03-27
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?
0
Comment
Question by:maqskywalker
2 Comments
 
LVL 15

Expert Comment

by:unknown_routine
ID: 39959859
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.
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39960499
Also get rid of while loop, it only complicate your code and you don not need it here.
Actually, you will still need while loops, and actually you will need 2.

What unknown_routine said about the separate try..catch blocks is true, but you will also need to change your while loop so there is a while loop around each of the new try..catch blocks. Think of it like this... "while" the user HASN'T entered a valid number in the first window, keeping asking them until they do. And then... "while" the user HASN'T entered a valid number for the second number, keep asking them until they do. Now that you have 2 valid numbers, perform your arithmetic.

I am guessing that this might be a homework/assignment related question, and if so we can't just give you the code to fix this. But have a good at the suggestions that we have made and if there are any further issues, post back here and we can help further!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question