How to Make Sure an Input Is a Number in Java
When you write Java programs there are inevitably times when you have to deal with input you have little information about. If you have input passed into your program or into a method within it and you need to make sure it is a number you can use methods of the numerical wrapper classes. These classes provide methods for parsing strings as number types which throw exceptions if the passed input is not numerical. By using these methods in conjunction with try and catch blocks you can work out what kinds of input value you are dealing with.
Instructions
-
-
1
Acquire a reference to the input value you want to check. Depending on your program you may already have the input stored as a variable. The following sample code creates a sample string variable to demonstrate the technique:
String input = "9";This variable stores the number nine as a string. The methods for parsing number types take string parameters so if your input is not currently stored as a string create a string to include it as in the following example:
int myNumber = 6;
String input = "" + myNumber; -
2
Create a try code block for your number parsing process. Enter the following outline of a try block:
try {
//parse here
}Inside this block you can place the code to parse your input. If you are programming in an Integrated Development Environment you will need to include a try block or the number parsing process will prevent your program from compiling. The try block is a way of safeguarding your application against number format exceptions.
-
-
3
Add a catch block to your code. If the code inside your try block does throw a number format exception, processing will jump immediately to the catch block. Add it after your try block closes as follows:
catch(NumberFormatException nfe)
{ System.out.println("Not a number: " + nfe.getMessage()); }If the input is not a number the content of this block will execute. At the moment it contains a simple output statement to the console but you can add your own processing inside the block.
-
4
Attempt to parse your input as a number. The number wrapper classes allow you to parse strings as different numeric types including integers, double precision, short, long and floating point numbers. Choose whichever class suits your program and enter it inside the try block. The following sample code demonstrates the technique for integers:
int inputNum = Integer.parseInt(input);
System.out.println("Is a number: " + inputNum);If the input is a number the second line will execute and the code will ignore the catch block. If the input is not a number the line after the parse attempt will not execute as processing will immediately move to the catch block.
-
5
Save your Java files, compile and run your program. Test it by altering the value of the original input variable as in the following example code:
String input = "p";This will cause the program to throw an exception and the content of the catch block will execute. Within the try block enter the code you want to execute when the input is a number. Within the catch block enter the code you want to execute if it is not.
-
1
Tips & Warnings
If you want to detect any type of number you can use multiple parsing operations one for each wrapper class.
If you are unsure of data input within your program you will need to test it extensively.
References
Resources
- Photo Credit Jupiterimages/Photos.com/Getty Images