Solved

java try and catch block

Posted on 2014-03-27
2
384 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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now