Scanner will fail to read integer for a specific data. Please see the variosu inputs.
code used:-
import java.io.*;
import java.util.*;
/*
This should test the various inputs of integers and longs.
*/
public class Test {
public static void main(String[] args) {
try {
System.out.println(" Enter Integer ");
Scanner scanner = Scanner.create(System.in);
int inte = scanner.nextInt();
System.out.println(" Enter Long ");
long lng = scanner.nextLong();
} catch(Exception exp) {
exp.printStackTrace();
}
}
}
output screen :-
vishalb:/home/vv145429/tiger/src/scanning/Assertion2 44 % java Test
Enter Integer
4578
Enter Long
012345
vishalb:/home/vv145429/tiger/src/scanning/Assertion2 45 % java Test
Enter Integer
04578
java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:761)
at java.util.Scanner.next(Scanner.java:1235)
at java.util.Scanner.nextInt(Scanner.java:1623)
at Test.main(Test.java:14)
vishalb:/home/vv145429/tiger/src/scanning/Assertion2 46 % java Test
Enter Integer
01234
Enter Long
0012345
vishalb:/home/vv145429/tiger/src/scanning/Assertion2 47 % java Test
Enter Integer
05555
Enter Long
-123456
vishalb:/home/vv145429/tiger/src/scanning/Assertion2 48 % java Test
Enter Integer
005555
Enter Long
-0123456
If u look at the data 01234 <04578 <05555. it fails at 04578 and accepts if u don;t use 0 and it will accept 0 for other two numbers.
code used:-
import java.io.*;
import java.util.*;
/*
This should test the various inputs of integers and longs.
*/
public class Test {
public static void main(String[] args) {
try {
System.out.println(" Enter Integer ");
Scanner scanner = Scanner.create(System.in);
int inte = scanner.nextInt();
System.out.println(" Enter Long ");
long lng = scanner.nextLong();
} catch(Exception exp) {
exp.printStackTrace();
}
}
}
output screen :-
vishalb:/home/vv145429/tiger/src/scanning/Assertion2 44 % java Test
Enter Integer
4578
Enter Long
012345
vishalb:/home/vv145429/tiger/src/scanning/Assertion2 45 % java Test
Enter Integer
04578
java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:761)
at java.util.Scanner.next(Scanner.java:1235)
at java.util.Scanner.nextInt(Scanner.java:1623)
at Test.main(Test.java:14)
vishalb:/home/vv145429/tiger/src/scanning/Assertion2 46 % java Test
Enter Integer
01234
Enter Long
0012345
vishalb:/home/vv145429/tiger/src/scanning/Assertion2 47 % java Test
Enter Integer
05555
Enter Long
-123456
vishalb:/home/vv145429/tiger/src/scanning/Assertion2 48 % java Test
Enter Integer
005555
Enter Long
-0123456
If u look at the data 01234 <04578 <05555. it fails at 04578 and accepts if u don;t use 0 and it will accept 0 for other two numbers.