Skip to content
Advertisement

I can’t insert more than 10 digit of integer type of data in java

I had a problem by my code. How to insert more than 10 digits of integer, I have searched and can be done by using BigInteger, but I don’t know how to use it in my code using “System.in”

package t3;
import java.util.Scanner;
import java.util.Date;
public class t3 {
    public static void main(String[]args){
        Date date= new Date();
        System.out.println(date.toString());
        int NIM;
        Scanner input = new Scanner(System.in);
        
        
        System.out.print("insert nim: ");
        NIM = input.nextInt();
        input.close();
        System.out.print(NIM);
    }

}

can someone help me? thank you before This is my code This my error

Advertisement

Answer

Simply because int max value is limited to 10 digits. You can use long instead as you can add upto 19 digits. Listed below is the difference of Integer and Long max and min values.

Integer.MIN_VALUE: -2 147 483 648
Integer.MAX_VALUE:  2 147 483 647

Long.MIN_VALUE: -9 223 372 036 854 775 808
Long.MAX_VALUE:  9 223 372 036 854 775 807

In your code, just simply replace with the following.

// int NIM; -- you can replace it into long
long NIM;

// NIM = input.nextInt(); -- call method nextLong instead.
NIM = input.nextLong();
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement