Solved

java try and catch block

Posted on 2014-03-27
2
389 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 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

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 129
tomcat startup error 5 136
Java syntax, or is it Selenium 6 43
learn programming 8 72
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 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