Solved

java try and catch block

Posted on 2014-03-27
2
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 36

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

688 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