Integer too long Java -


this question has answer here:

  public static void gramstoatoms()   {    system.out.println("enter amount of grams");    scanner keyboard = new scanner(system.in);    long x = keyboard.nextlong();    system.out.println("enter unit grams");    long y = keyboard.nextlong();    long result = x/y;    long answer = result*60200000000000000000000;    system.out.println(answer + "atoms");   } 

how change code don't integer long error?

just use biginteger instead of long

biginteger bi=new biginteger("6020000000000000000000"); 

for method:

public static void gramstoatoms()   {    system.out.println("enter amount of grams");    scanner keyboard = new scanner(system.in);    string x = keyboard.nextline();    system.out.println("enter unit grams");    string y = keyboard.nextline();    biginteger result = new biginteger(x).divide(new biginteger(y));    biginteger answer = result.multiply(new biginteger("60200000000000000000000"));    system.out.println(answer + "atoms");   } 

and if x , y small, can use them simple long, when multiply 602.......0000 use biginteger


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -