try catch my java quiz question

mossman

Gawd
Joined
Feb 10, 2002
Messages
664
I'm in the process of making a java quiz, when I noticed that I get an error if someone enters anything besides a number. Solution- I need a try catch statement.

Could someone show me the most effecient way to put that it my code?

Code:
// Question #1
++counter1;
++counter2;
						      	
value1 = 1 + ( int ) ( Math.random() * 50 );						      	
value2 = 1 + ( int ) ( Math.random() * 50 );						      	
value3 = 1 + ( int ) ( Math.random() * 50 );						      	
value4 = 1 + ( int ) ( Math.random() * 50 );						      	
value5 = 1 + ( int ) ( Math.random() * 50 );						      	
					      
zVal = JOptionPane.showInputDialog("What is the value of X in the following equation?\n\n"+"X = "+value1 + "+" +value2 +						      	
		"%" +value3 + "*" +value4 +";");				      		 
						      	
z= Integer.parseInt( zVal);							    
							      
output = value1  +  value2 % value3 * value4;
							      
if (z==output)
{
	JOptionPane.showMessageDialog( null,"Very Good!\nThe answer was in fact " + output,"Result",
	JOptionPane.INFORMATION_MESSAGE );
	++counter3;
							      	  
}
							      
else
{
	JOptionPane.showMessageDialog( null,"Incorrect\nThe answer was " + output,"Result",
	JOptionPane.ERROR_MESSAGE );
}
 
It's been awhile since I've done java, but

Code:
// Question #1
++counter1;
++counter2;
						      	
value1 = 1 + ( int ) ( Math.random() * 50 );						      	
value2 = 1 + ( int ) ( Math.random() * 50 );						      	
value3 = 1 + ( int ) ( Math.random() * 50 );						      	
value4 = 1 + ( int ) ( Math.random() * 50 );						      	
value5 = 1 + ( int ) ( Math.random() * 50 );						      	
					      
zVal = JOptionPane.showInputDialog("What is the value of X in the following equation?\n\n"+"X = "+value1 + "+" +value2 +	 "%" +value3 + "*" +value4 +";");

 try{	
    z= Integer.parseInt( zVal);							    
							      
    output = value1  +  value2 % value3 * value4;
							      
    if (z==output)
    {
	JOptionPane.showMessageDialog( null,"Very Good!\nThe answer was in fact " + output,"Result",
	JOptionPane.INFORMATION_MESSAGE );
	++counter3;
							      	  
    }
							      
    else
    {
	JOptionPane.showMessageDialog( null,"Incorrect\nThe answer was " + output,"Result",
	JOptionPane.ERROR_MESSAGE );
    }
}
catch(NumberFormatException e){
    System.out.println("NumberFormatException: Please enter a valid integer");
    // do what you want here, probably recall the function so they can reinput their answer?

}
 
Here's a simple yet very crude way to do it.

Code:
try
{
	z = Integer.parseInt(zVal);
}
catch(Exception e)
{
	// Maybe even show a msgbox that tells the user to enter a correct number.
	return;
}

I'm sure you expected a little more :D

Nemezer
 
Hehee, I remember when my programs were one big public static void main method :p

As for your current problem... Maybe it's because it's to early in the morning but I think I'm seing double :eek:

Code:
...

i = Integer.parseInt( iVal );

try
{
	i = Integer.parseInt( iVal );

}
catch ( NumberFormatException ross)

...

Nemezer
 
One way certainly is to catch the exception. Another, exceptionless way is:

Code:
// Question #1
++counter1;
++counter2;
						      	
value1 = 1 + ( int ) ( Math.random() * 50 );						      	
value2 = 1 + ( int ) ( Math.random() * 50 );						      	
value3 = 1 + ( int ) ( Math.random() * 50 );						      	
value4 = 1 + ( int ) ( Math.random() * 50 );						      	
value5 = 1 + ( int ) ( Math.random() * 50 );						      	
					      
zVal = JOptionPane.showInputDialog("What is the value of X in the following equation?\n\n"+"X = "+value1 + "+" +value2 +						      	
		"%" +value3 + "*" +value4 +";");				      		 
						      	
output = value1  +  value2 % value3 * value4;
							      
if (String.valueOf(output).equals(String.valueOf(zVal).trim()))
{
	JOptionPane.showMessageDialog( null,"Very Good!\nThe answer was in fact " + output,"Result",
	JOptionPane.INFORMATION_MESSAGE );
	++counter3;
							      	  
}
							      
else
{
	JOptionPane.showMessageDialog( null,"Incorrect\nThe answer was " + output,"Result",
	JOptionPane.ERROR_MESSAGE );
}
 
Back
Top