java - what is the best way to solve the big number string to long? -


i working problem.the following code works small string in case have work long numbers.what best possible solution ?

it gives error long string how approach solve best possible way. way uva424. there many solution online .i solve way portion of code

import java.util.scanner;  public class integerinquiry {  public static void main(string args[]) {     scanner input = new scanner(system.in);     long total = 0;     while (input.hasnext()) {         string = input.next();      if(i.equals("0"))  break;           int size = i.length();         if(size <=100 ){         long = long.parselong(i);         total = total+a;         }        }     system.out.println(total);  } } 

for big numbers can use bigdecimal

    scanner input = new scanner(system.in);     bigdecimal bigdecimal = new bigdecimal(0);     while (input.hasnext()) {         string = input.next();         if (i.equals("0"))             break;         bigdecimal = bigdecimal.add(new bigdecimal(i));     }     input.close();     system.out.println("bigdecimal " + bigdecimal);     system.out.println("long " + bigdecimal.longvalue()); // caution - narrowing  

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -