无法运行以下程序:NoSuchElementException

时间:2014-07-04 04:00:19

标签: java

在这里输入代码

public static void main(String[] args) 
    {
SecureRandom r = new SecureRandom();

        int bitsize = 16;
        RsaPrng rsa1=new RsaPrng();


    BigInteger q = RsaPrng.generateN(bitsize, r);
    System.out.println(" q is = "+q);


        BigInteger p = RsaPrng.getPrime(bitsize, r);
    System.out.println(" p is = "+p);


BigInteger n = RsaPrng.generaten(p, q);
System.out.println(" n is = "+n);

BigInteger phi_n = RsaPrng.generate_phin(p, q); 
    System.out.println(" phi_n is = "+phi_n);


        BigInteger b = RsaPrng.get_b(bitsize, r, phi_n);
    System.out.println(" b is = "+b);


int bb = b.intValue();


BigInteger seed= new BigInteger ("34567");      
System.out.println(" seed is = "+seed);

BigInteger bitstream;


System.out.println("Please. Generate Five values\n");

Scanner sc = new Scanner(System.in);


BigInteger count = BigInteger.valueOf(1);

System.out.println("n \t  x  \t   y");


System.out.println("\t   \t \t    \t \t ");


    for (BigInteger bi = sc.nextBigInteger() ;
    bi.compareTo(BigInteger.ZERO) > 0;

bi = bi.subtract(BigInteger.ONE) )

    {
            seed=seed.pow(bb).mod(n);


            bitstream=seed.mod(TWO);

System.out.println(count+"\t" +seed +"\t "+bitstream);

count=count.add(BigInteger.ONE);

  }
        }
    }

这是我的代码。当我试图运行此代码时,我有以下错误

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1530)``
    at java.util.Scanner.nextBigInteger(Scanner.java:2559)
    at java.util.Scanner.nextBigInteger(Scanner.java:2523)
    at rsaPRNG.RsaPrng.main(RsaPrng.java:188)

我没有得到我犯错的地方。

1 个答案:

答案 0 :(得分:1)

问题是你是直接调用sc.nextBigInteger();做什么是将输入的下一个标记扫描为BigInteger。现在不检查你是否有下一个令牌。你正试图操纵它,所以在这种情况下它失败了你能做的就是先调用scannerObject.hasNext(),如果这个扫描器的输入中有另一个标记,它将返回true。 然后你使用scanner.hasNextBigInteger()检查下一个令牌是否是BigInteger,然后尝试操作