I've used the length function to calculate the length of digits in an integer input in my program. But when I enter the condition in if syntax, it specifically accepts 1234567890 as input. More clearly, if I enter any random 10 digit no. it shows error whereas on entering 1234567890 it works. My code snippet:
import java.util.*;
public class tryy {
public static void main(String[] args) {
System.out.println("Mobile Number");
Scanner sc1= new Scanner(System.in);
int number = sc1.nextInt();
int length = String.valueOf(number).length();
if (length != 10){
System.out.println("Invalid Mobile number!");
System.exit(0);
}
}
}
Output when entering any 10 digit random number:
Mobile Number
6483928564
Exception in thread "main" java.util.InputMismatchException: For input string: "6483928564"
at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at tryy.main(tryy.java:6)