how to input a BigInteger type in java

前端 未结 5 1201
抹茶落季
抹茶落季 2020-12-31 13:34

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();
         


        
相关标签:
5条回答
  • 2020-12-31 14:12

    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
    }
    
    0 讨论(0)
  • 2020-12-31 14:14

    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.

    0 讨论(0)
  • 2020-12-31 14:20
    Scanner sc = new Scanner(System.in);
    BigInteger b = new BigInteger(sc.next());
    

    There is also:

    BigInteger b = sc.nextBigInteger();
    
    0 讨论(0)
  • 2020-12-31 14:24

    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();
    
    0 讨论(0)
  • 2020-12-31 14:30
    Scanner sc = new Scanner(System.in);
    BigInteger bi = sc.nextBigInteger();
    

    Reference: Scanner#nextBigInteger

    0 讨论(0)
提交回复
热议问题