Try statement with a number exception
I am trying to write the code for a program that you creat multiplication tables. The user enters the number and takes the number and multiples it by 1 all the way to 12.
I need to tell it via a TRY statement not to except anything other than an integer. I can't seem to fiuge out how to write the try statement.
Try
{
varname = Integer.parseInt(varname);
}
or I am forgetting something.
you need a catch:
try {
varname = Integer.parseInt(varname);
} catch (NumberFormatException e) {
System.err.println("Ahhh this is not a number!");
}
Also, please use the code format tags
zadoka at 2007-7-14 19:18:20 >

i did that here is what i get no matter what the name of the second variable is?
symbol : variable inputData
location: class Multiply
multiplier = Integer.parseInt(inputData);
ymbol : variable mulitplier
location: class Multiply
multiplier = Integer.parseInt(mulitplier);
the karet is pointing to the first letter in the varname in ().
> i did that here is what i get no matter what the name
> of the second variable is?
>
> symbol : variable inputData
> location: class Multiply
> multiplier = Integer.parseInt(inputData);
>
> ymbol : variable mulitplier
> location: class Multiply
> multiplier = Integer.parseInt(mulitplier);
> the karet is pointing to the first letter in the
> varname in ().
Please post your code and the complete error message. And use code formatting tags. Thanks
zadoka at 2007-7-14 19:18:20 >

here is the code
import java.io.*;
public class Multiply
{
public static void main (String[] args ) throws IOException
{
//Declaring variables
int multiplier;
int correct;
boolean done = false;
//Opening messages
System.out.println("\t\tWelcome to the Multiplication Quiz");
System.out.println("");
//Calling the user-defined methods
try
{
multiplier = Integer.parseInt(mulitplier);
}
catch(NumberFormatException e)
{
System.out.println("\t\tThis is not a valid number.");
}
multiplier = getNumber();
correct = takeQuiz(multiplier);
System.out.println("\t\tYou got "+correct+ " correct!" );
}
public static int getNumber() throws IOException
{
//Declaring variables
BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
String inputData;
int multiplier;
//Get a value from user
System.out.println("Enter the multiplication table you wish to practice:");
inputData = dataIn.readLine();
multiplier = Integer.parseInt( inputData );
//Return a value to main
return multiplier;
}
public static int takeQuiz(int multiplier) throws IOException
{
//Delcaring variablaes
BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
String inputData;
int answer;
int count = 0;
int correct = 0;
while ( count <= 12)
{
//Display question and get answer
System.out.println( "What is "+count + " times " + multiplier +"?" );
inputData = dataIn.readLine();
answer= Integer.parseInt( inputData );
if (answer == count * multiplier)
{
System.out.println("\tCorrect!");
correct = correct + 1;
}
else
{
System.out.println("\tIncorrect");
}
count = count + 1;
}
return correct;
}
}
Does not compile because multiplier is mispelled here:multiplier = Integer.parseInt(mulitplier);but once you fix that it still won't compile because you are trying to parse and int to an int.
zadoka at 2007-7-14 19:18:20 >

Ok, thank you!! I'm trying to set this up so that if someone enter say a letter or a decimal number i.e. 5.6. to catch it and return the message below. I need to use the try statement though.
how should I code this? should it be
a double I am lost PLEASE HELP ME!!
Message was edited by:
Java_n00blet
I made some crude modifications to your code, but it is closer in the direction of what you are looking for. See if you can improve it further:
class Multiply
{
public static void main (String[] args ) throws IOException
{
//Declaring variables
int multiplier;
int correct;
boolean done = false;
//Opening messages
JOptionPane.showMessageDialog(null, "Welcome to the Multiplication Quiz");
System.out.println("");
//Calling the user-defined methods
try
{
multiplier = Integer.parseInt(getNumber());
correct = takeQuiz(multiplier);
System.out.println("\t\tYou got "+correct+ " correct!" );
}
catch(NumberFormatException e)
{
System.out.println("\t\tThis is not a valid number.");
}
}
public static String getNumber() throws IOException
{
return JOptionPane.showInputDialog(null,
"Enter the multiplication table you wish to practice:",
"Enter Table", JOptionPane.QUESTION_MESSAGE);
}
public static int takeQuiz(int multiplier) throws IOException
{
//Delcaring variablaes
BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
String inputData;
int answer;
int count = 0;
int correct = 0;
while ( count <= 12)
{
//Display question and get answer
System.out.println( "What is "+count + " times " + multiplier +"?" );
inputData = dataIn.readLine();
answer= Integer.parseInt( inputData );
if (answer == count * multiplier)
{
System.out.println("\tCorrect!");
correct = correct + 1;
}
else
{
System.out.println("\tIncorrect");
}
count = count + 1;
}
return correct;
}
}
zadoka at 2007-7-14 19:18:20 >

