简体   繁体   中英

how to input a BigInteger type in 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?

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

Reference: Scanner#nextBigInteger

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

There is also:

BigInteger b = sc.nextBigInteger();

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.

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
}

You need to import the BigInteger package:

import java.math.BigInteger;

Include this package and code below:-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM