简体   繁体   English

Java错误“线程中的异常”主“java.util.NoSuchElementException”

[英]Java Error “Exception in thread ”main“ java.util.NoSuchElementException”

I am getting this error: 我收到此错误:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at IntegerSequence.main(IntegerSequence.java:73)

This is my first lab trying to read input from a text file and submit output to another text file. 这是我的第一个尝试从文本文件中读取输入并将输出提交到另一个文本文件的实验室。 I need it to run 5 different 'for' loops. 我需要它来运行5个不同的'for'循环。 I thought I was close, and then I got the error. 我以为我很亲密,然后我收到了错误。 My code is as follows: 我的代码如下:

public static void main(final String[] args) throws IOException {

    File myFile = new File("input.txt");
    Scanner inputFile = new Scanner(myFile);
    PrintWriter outputFile = new PrintWriter("output.txt");

    // set accumulator
    int sum = 0;

    double iterations = inputFile.nextDouble();

    for (int start = 1; start <= iterations; start++) { 

        int starting = inputFile.nextInt();
        int ending = inputFile.nextInt();

        for (starting = 0 + starting; starting <= ending; starting++) {

            sum = sum + starting;
        }
    }
    outputFile.println(iterations); 
    outputFile.println(sum);


    outputFile.close();
    inputFile.close();
        }
}

Thank you for your help! 谢谢您的帮助!

The input file is: 5 1 10 1 15 输入文件是:5 1 10 1 15

Your first number should be 2 then, since you only have two sets of starting and ending values. 那么你的第一个数字应该是2,因为你只有两组startingending值。 Or, you just need to provide three more pairs of values. 或者,您只需要提供三对值。

But, ideally you should program defensively with hasNextXYZ() checks for the kind of value you're about to read with a corresponding nextXYZ() read call or you run the risk of getting a NoSuchElementException . 但是,理想情况下,您应该使用hasNextXYZ()检查防御性程序,以检查您将要使用相应的nextXYZ()读取调用读取的值的类型,否则您将面临获取NoSuchElementException的风险。

Also, you're dealing exclusively with int s here. 另外,你在这里专门处理int So, you should use int as the data type and call hasNextInt() and nextInt() only. 因此,您应该使用int作为数据类型,并仅调用hasNextInt()nextInt()

And since, starting = 0 + starting; 从那以后, starting = 0 + starting; seems to be doing nothing, the for loop can simply be 似乎什么都不做, for循环可以简单

for (; starting <= ending; starting++) {
    sum += starting;
}

ie the initialization expression in a for loop is optional. for循环中的初始化表达式是可选的。


To calculate the sums separately instead of a cumulative total, you need to move the sum and it's associated println() calls to inside the loop. 要分别计算总和而不是累计总数,您需要移动sum并将其关联的println()调用到循环内部。

int iterations = inputFile.nextInt();
outputFile.println(iterations); 

for (int start = 1; start <= iterations; start++) { 

    int starting = inputFile.nextInt();
    int ending = inputFile.nextInt();

    int sum = 0; // reset sum to 0

    for (; starting <= ending; starting++) {
        sum += starting;
    }
    outputFile.println(sum);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java扫描器错误:线程“主”中的异常java.util.NoSuchElementException - Java Scanner Error: Exception in thread “main” java.util.NoSuchElementException 线程“主”中的Java异常java.util.NoSuchElementException运行时错误 - Java Exception in thread “main” java.util.NoSuchElementException runtime error Java错误-“线程“ main”中的异常” java.util.NoSuchElementException - Java Error - “Exception in thread ”main" java.util.NoSuchElementException 线程“主”中的异常java.util.NoSuchElementException? - Exception in thread “main” java.util.NoSuchElementException? 线程“主”中的异常java.util.NoSuchElementException - Exception in thread “main” java.util.NoSuchElementException 线程“主”中的异常java.util.NoSuchElementException - Exception in thread “main” java.util.NoSuchElementException 主线程java.util.NoSuchElementException中的异常 - Exception in main thread java.util.NoSuchElementException 线程“main”中的异常 java.util.NoSuchElementException - Exception in thread "main" java.util.NoSuchElementException 线程“main”中的异常 java.util.NoSuchElementException(如何修复错误) - Exception in thread "main" java.util.NoSuchElementException (how to fix error) 线程“主”中的异常java.util.NoSuchElementException错误 - Exception in thread “main” java.util.NoSuchElementException Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM