Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to input a BigInteger type in java

Tags:

java

when I tried to get an input of type Integer, what I only needed to do was the code below.

Scanner sc = new Scanner(System.in);
int N = sc.nextInt();

but when it comes to BigInteger, I don't know what to do. What can I do to read a BigInteger Type input from the user?

like image 447
hongtaesuk Avatar asked Aug 11 '11 06:08

hongtaesuk


4 Answers

Scanner sc = new Scanner(System.in);
BigInteger bi = sc.nextBigInteger();

Reference: Scanner#nextBigInteger

like image 164
dertkw Avatar answered Nov 18 '22 09:11

dertkw


Scanner sc = new Scanner(System.in);
BigInteger b = new BigInteger(sc.next());

There is also:

BigInteger b = sc.nextBigInteger();
like image 41
Robert Balent Avatar answered Nov 18 '22 10:11

Robert Balent


How about

Scanner.nextBigInteger()

But I have to recommend that you read the documentation rather than ask this question. You harm yourself by not researching.

like image 41
djna Avatar answered Nov 18 '22 09:11

djna


Make sure you prepare to catch an exception from the BigInteger - if the scanner fails to find a string you might get a BigInteger with non integer characters and it'll throw an exception.

Scanner scanner = new Scanner(fileOrOther);
try{
     BigInteger bigint = scanner.nextBigInteger();
} catch(NumberFormatException ex) {
//handle Code here
}
like image 36
Mgamerz Avatar answered Nov 18 '22 10:11

Mgamerz