Use Scanner.hasNextInt():
Returns
trueif the next token in this scanner's input can be interpreted as anintvalue in the default radix using thenextInt()method. The scanner does not advance past any input.
Here's a snippet to illustrate:
Scanner sc = new Scanner(System.in);
System.out.print("Enter number 1: ");
while (!sc.hasNextInt()) sc.next();
int num1 = sc.nextInt();
int num2;
System.out.print("Enter number 2: ");
do {
while (!sc.hasNextInt()) sc.next();
num2 = sc.nextInt();
} while (num2 < num1);
System.out.println(num1 + " " + num2);
You don't have to parseInt or worry about NumberFormatException. Note that since the hasNextXXX methods don't advance past any input, you may have to call next() if you want to skip past the "garbage", as shown above.
Related questions
- How do I keep a scanner from throwing exceptions when the wrong type is entered? (java)
io - How to read integer value from the standard input in Java - Stack Overflow
java - How do I convert scanner input into an integer? - Stack Overflow
scan.nextInt vs Integer.valueOf(scan.nextLine())
My Java code will not wait for user input when I ask the user for a new input
See The Scanner class and its caveats from our wiki.
It explains your problem and the solution to it.
More on reddit.comUse Scanner.hasNextInt():
Returns
trueif the next token in this scanner's input can be interpreted as anintvalue in the default radix using thenextInt()method. The scanner does not advance past any input.
Here's a snippet to illustrate:
Scanner sc = new Scanner(System.in);
System.out.print("Enter number 1: ");
while (!sc.hasNextInt()) sc.next();
int num1 = sc.nextInt();
int num2;
System.out.print("Enter number 2: ");
do {
while (!sc.hasNextInt()) sc.next();
num2 = sc.nextInt();
} while (num2 < num1);
System.out.println(num1 + " " + num2);
You don't have to parseInt or worry about NumberFormatException. Note that since the hasNextXXX methods don't advance past any input, you may have to call next() if you want to skip past the "garbage", as shown above.
Related questions
- How do I keep a scanner from throwing exceptions when the wrong type is entered? (java)
- the condition num2 < num1 should be num2 <= num1 if num2 has to be greater than num1
- not knowing what the kb object is, I'd read a
Stringand thentryingInteger.parseInt()and if you don'tcatchan exception then it's a number, if you do, read a new one, maybe by setting num2 to Integer.MIN_VALUE and using the same type of logic in your example.
You can use java.util.Scanner (API):
import java.util.Scanner;
//...
Scanner in = new Scanner(System.in);
int num = in.nextInt();
It can also tokenize input with regular expression, etc. The API has examples and there are many others in this site (e.g. How do I keep a scanner from throwing exceptions when the wrong type is entered?).
If you are using Java 6, you can use the following oneliner to read an integer from console:
int n = Integer.parseInt(System.console().readLine());
Try this code
package exmaple;
import java.util.Scanner;
public class Parser {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name = in.next();
try{
int result = Integer.parseInt(name);
if(result / 2 == 1) {
System.out.println("a");
}
} catch(Exception exception) {
}
in.close();
}
}
Try
System.out.println("Enter first number");
Scanner sc=new Scanner(System.in);
int name=sc.nextInt();
if ((name / 2) == 1)
System.out.println("a");
RUN
run:
Enter first number
2
a