Some Number Format Exceptions

 

Some Number Format Exceptions.  Now we need to quickly survey a couple programs that trap for NumberFormatExceptions.  These are particularly important for dealing with non-numeric or blank inputs from a user.

Remember that we stated in the last webpage that when a Java program encounters something that doesn't quite fit the required format or structure the program is said to throw an exception.  You should also remember that

  • the source of the exceptions we are trying to handle is placed within a try block
  • the ways we want to handle these exceptions are placed in catch blocks

Our first program returns to one of our earlier programs where we got the user to input two integers and then returned their product while echoing the inputs.  Remember how it crashed when we gave it anything other than the correct types of inputs.  We will reorganize it and put in some try and catch blocks to reduce the crashes and give feedback to the user about how they need to change their inputs.

The following program is again an application, it should be quite easy to convert it to an applet.  You should call the file IntegerFormatExceptionPrimitive.java.  I put primitive in the name because we will improve on the code in future web pages as we learn more Java.

 

import javax.swing.JOptionPane;

public class IntegerFormatExceptionPrimitive
{

public static void main( String args[] )
{

String firstUserEntry, secondUserEntry;
int firstInteger = 0;
int secondInteger = 0;
int result;

// Prompt user for input
firstUserEntry = JOptionPane.showInputDialog("Please enter an integer:");

// convert these string values to integers
try
{

firstInteger = Integer.parseInt(firstUserEntry);

}

catch (NumberFormatException nfeInteger)
{

JOptionPane.showMessageDialog( null, "The first entry needs to be an integer", "Input Error", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);

}

// prompt user for second input
secondUserEntry = JOptionPane.showInputDialog("Please enter another integer:");

try
{

secondInteger = Integer.parseInt(secondUserEntry);

}

catch (NumberFormatException nfeInteger)
{

JOptionPane.showMessageDialog( null, "The second entry needs to be an integer", "Input Error", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);

}

// if the program obtains both inputs and can convert them to integers
// then this section of code will execute

result = firstInteger * secondInteger;

JOptionPane.showMessageDialog( null, "The product of the values you entered " + firstInteger +
" and " + secondInteger + ", is " + result,
"Integer Product", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);

} // end method main

} // end class IntegerFormatExceptionPrimitive

 

Now you should compile the program and execute it.  Hopefully, you have learned the steps to do this by now.

At a general level, I have reorganized the code so that it

  1. first deals with obtaining the first integer. 
    1. If it can't, the program throws and error and catches it. 
    2. The user gets a message and the program stops cleanly after telling them what they need to do. 
  2. If they enter the first input correctly the program reaches the section of code where it tries to obtain the second integer from the user, 
    1. If this fails the program throws an error and catches it. 
    2. Then the program gives the user a message telling them what they need to do.
  3. If the user enters both inputs correctly then the solution is computed and displayed to the user while echoing the inputs.

Notice how the parse statements are within the try blocks.  Notice how the catch blocks are looking for NumberFormatExceptions.

Now you should run the program trying a number of different inputs such as

  • first entry blank
  • first entry is a letter
  • first entry is 45.6
  • first entry is 45 and second entry is blank
  • first entry is 45 and second entry is a letter
  • first entry is 45 and second entry is 45.6
  • first entry is 34 and second entry is 75

The results should surprise you.  It would be nice to be able to do more, like get both inputs from the user before giving them an error message.  But that requires some more Java code.

Floating Point Exceptions.  Now we will modify this same program slightly to deal with floating point numbers.  You should call this file FloatFormatExceptionPrimitive.java.

This is directly analogous to the previous program except that we are looking for floating point numbers.  In reality, parsing floats has considerably more capacity for handling a variety of user inputs.  Thus, in order to get the program to throw an error your going to need to enter text or a blank.

 

import javax.swing.JOptionPane;

public class FloatFormatExceptionPrimitive
{

public static void main( String args[] )
{

String firstUserEntry, secondUserEntry;
float firstNumber = 0;
float secondNumber = 0;
float result;
String errorMessage = "";

// Prompt user for input
firstUserEntry = JOptionPane.showInputDialog("Please enter a number:");

// convert these string values to integers
try
{

firstNumber = Float.parseFloat(firstUserEntry);

}

catch (NumberFormatException nfeFloat)
{

JOptionPane.showMessageDialog( null, "The first entry needs to be a number", "Float Product", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);

}

// prompt user for second input
secondUserEntry = JOptionPane.showInputDialog("Please enter another number:");

try
{

secondNumber = Float.parseFloat(secondUserEntry);

}

catch (NumberFormatException nfeFloat)
{

JOptionPane.showMessageDialog( null, "The second entry needs to be a number", "Float Product", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);

}

// if the program obtains both inputs and can convert them to integers
// then this section of code will execute

result = firstNumber * secondNumber;

JOptionPane.showMessageDialog( null, "The product of the values you entered " + firstNumber +" and " + secondNumber + ", is " + result,
"Float Product", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);

} // end method main

} // end class FloatFormatExceptionPrimitive

 

Much more will be said about the code in class, but there are just a few differences between this and the previous program having to do with dealing with floating point numbers.

It should be fairly obvious that you could easily convert this to trap for doubles.  Nor would it take much to convert these into applets.