Reputation: 91
I am trying to prompt a user with the value they inputted and an error message if it's not an integer. When I try to prompt them, their input stays 0 when the input is a double or string.
//main method
public static void main(String[] args) {
int i = 0;
//instantiate new Scanner for user input
Scanner input = new Scanner(System.in);
//parse imput, display value
//and prompt user that their input is not a int
try {
inputNum = Integer.parseInt(input.next());
System.out.println("Value entered is " +
String.valueOf(inputNum));
} catch(NumberFormatException e) {
System.out.println("Value entered is " +
String.valueOf(inputNum));
System.out.println(String.valueOf(inputNum) + " is not an integer.");
}
}
}
Upvotes: 2
Views: 93
Reputation: 1275
There is an another concise way to do it without throwing any exception, you can use hasNextInt()
to pre-validate if the input value is a valid integer before hand, then use nextInt()
to read an integer without parsing the string:
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int inputNum;
if(scanner.hasNextInt()){
inputNum = scanner.nextInt();
System.out.println("Value entered is " + inputNum);
}else{
System.out.println(scanner.next() + " is not an integer.");
}
}
Upvotes: 0
Reputation: 2728
I've modified the code to make it work according to your need:
public static void main(String[] args) {
//instantiate new Scanner for user input
Scanner input = new Scanner(System.in);
//parse imput, display value
//and prompt user that their input is not a int
String inputNum = input.next();
try {
System.out.println("Value entered is " +
Integer.parseInt(inputNum));
} catch (NumberFormatException e) {
System.out.println("Value entered is " +
inputNum);
System.out.println(inputNum + " is not an integer.");
}
}
Above is the standard approach to check if a String is an integer in java. If you want a simpler & powerful way you can leverage the Apache Commons library:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final String num = input.next();
// check to see if num is a number
if (NumberUtils.isCreatable(num)) {
System.out.println("Value entered is " + num);
} else {
System.out.println("Value entered is " + num);
System.out.println(num + " is not a number.");
}
}
Note that NumberUtils#isCreatable
checks for a wide variety of number formats(integer, float, scientific...)
If you want something equivalent to Integer#parseInt
, Long#parseLong
, Float#parseFloat
or Double#parseDouble
. Use instead, NumberUtils#isParsable
.
Upvotes: 0
Reputation: 102832
Well, it's a concept ('ask user for some input, keep asking if it is not valid') that you may want to do more than once, so it has no business being in main.
Give it its own method.
This method would take as input a 'prompt' and will return a number. The purpose is to ask the user (with that prompt) for a number, and to keep asking until they enter one.
You can use while()
to loop code until a certain condition is met, or simply forever, using return
to escape the loop and the entire 'ask the user for a number' method in one fell swoop.
Upvotes: 0
Reputation: 1007
If the input is a double or a string then parseInt
would throw an exception and inputNum
would not be assigned any new value. You could store input.next()
in a string before passing it to parseInt
- or you might be able to use e
in the catch block to figure out the bad value
String s;
//parse imput, display value
//and prompt user that their input is not a int
try {
s = input.next();
System.out.println("Value entered is " + s);
inputNum = Integer.parseInt(s);
} catch(NumberFormatException e) {
System.out.println(s + " is not an integer.");
}
}
}
Upvotes: 2