Question

I am trying to write a simple program which would generate randomly 3 integers, then put them in array and then concatenate them into a single sequence of integers, but it throws an error

here is the code:

int [] kol=new int[3];

for(int j=0;j<3;j++) {
    kol[j]=(int)Math.round(Math.random() * 89999) + 10000;              
    System.out.print(kol[j] +"\n" );                
}

String ma=kol[0]+","+kol[1]+","+kol[2]+";";
System.out.println(ma);

I also tried:

int b = Integer.parseInt(Integer.toString(kol[0]) + Integer.toString(kol[1]) +
    Integer.toString(kol[2]));
System.out.println(b);

but same error:

Exception in thread "main" java.lang.NumberFormatException: For input 
at java.lang.NumberFormatException.forInputString(Unknown Source)
string: "715534907077099"
Was it helpful?

Solution 2

In java, int type is a 32-bit signed value, of which the maximum value is 2147483647(2^31-1). Obviously your value "715534907077099" is much bigger than Integer.MAX_VALUE. If it's still less than Long.MAX_VALUE, int n = Long.parseLong(strValue) will work.

But if you don't want to have a up limit, use BigInteger instead:

BigInteger bi = new BigInteger(Integer.toString(kol[0]) + Integer.toString(kol[1]) +
    Integer.toString  (kol[2]));
System.out.println(bi);

It seem that I need to upload a pic to prove my code, according to the comments...

OTHER TIPS

Integer range is not enough to have your large values.

int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648

So instead use java.math.BigInteger for the same

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top