-3

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)

4 Answers4

2

The reason is that you have input value more than Integer.MAX_VALUE,check the Integer documentation,we can find that the max value of Integer is 2147483647,while your input is 6483928564,it's greater than the max value,which cause this problem.

In order to solve this problem,you can use long instead of int(keep in mind that long type also may face this issue)

long number = sc1.nextLong();
flyingfox
  • 13,414
  • 3
  • 24
  • 39
  • @user16320675 I think the suitable way is to use `scanner.nextByte()` to read it and then convert to to string, so that we can avoid to check if it's `int` or `long` – flyingfox Jul 31 '21 at 07:26
  • @user16320675 because I am not fully tested `scanner.nextByte()` so I just make it as a comment to you – flyingfox Jul 31 '21 at 07:32
  • @user16320675 because you add a comment to my answer, it's a good chance for us to learn and discuss – flyingfox Jul 31 '21 at 07:35
0

Here is your Solution Maximum value that a integer variable in java can accept is 2147483647 Use Double or Long Int

0

The problem is that, you are assigning a big number for integer type, so try long or another type.

import java.util.*;
public class Main {
    public static void main(String[] args) {
        System.out.println("Mobile Number");
                    Scanner sc1= new Scanner(System.in);
                    // int number = sc1.nextInt();
                    long number = 6483928564L;
                    
                    int length = String.valueOf(number).length();
                    if (length != 10){
                        System.out.println("Invalid Mobile number!");
                        System.exit(0);
                    }
                    else
                    {
                        System.out.println("No problem!");
                    }
    }
}
0

If you are not performing any calculations, then you can take the input as String, and check the length of it.

It will solve your dependency on data types' storage capacity.

You can also do pattern validations, for a phone number, if you take the input as a string.

But if you want to do calculations, then you can change the variable type to a higher type and it will solve your problem.