Java错误混淆NoSuchElementException:找不到行

时间:2013-12-30 13:59:22

标签: java

请有人帮忙解决我的代码中的以下错误,我不知道为什么一切看起来都会出现此错误。请简单解释一下错误,是运行时错误?,逻辑错误?,语法错误?,抱歉,我是编程新手,我只是想要好奇。

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at JavaAssigment3.main(JavaAssigment3.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 1

以下是我的代码,java版本是1.7,IDE是Intellj IDEA 13.0.1社区版

import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;


public class JavaAssigment3 {

    public static int tabSize = 5; //Determine length of the array

    public static void main(String [] args) throws InterruptedException {
        int maxInt;
        int minInt;
        double mean;
        double sDeviation;

        int[] tab = new int[tabSize];

        int error = tabSize;
        int errorEmptyArray = 0; //user insert strings without integers - array is empty.
        System.out.println("Please enter a series of integers: ("+tabSize+" numbers separated by white space)");
        do {
            if (error<tabSize || errorEmptyArray==tabSize){
                System.out.println("Input incomplete");
                System.out.println("Try again....need integers ...");
                System.out.println("Need " + error + " correct integers");
            } //if
            Scanner in = new Scanner(System.in);
            String numbers = in.nextLine();
            StringTokenizer st = new StringTokenizer(numbers);
            int x = 0;
            in.close();
            while (st.hasMoreTokens() && error > 0){
                try {
                    int i =Integer.parseInt((st.nextToken()).trim()); //the string to integer conversion
                    tab[x] = i;
                }
                catch (NumberFormatException nfe){
                    System.out.println(nfe.getMessage() + " <- not proper integer.");
                    errorEmptyArray = tabSize; //set to tabSize if the array is empty on input
                }
            x++;
            }
        } while (error != 0);
        //sort the array order ins ascending order
        Arrays.sort(tab);
        //largest number is the last element in the array
        maxInt = tab[tab.length - 1];
        //largest number is the first element in the array
        minInt = tab[0];

        //calculate the mean
        int sum = 0;
        for (int y : tab){ sum = sum + y; }
        mean = ((double) sum / tab.length);

        //calculate the standard deviation
        double gSum = 0;
        for (double z : tab){ gSum = gSum +((z - mean) * (z - mean)); }
        sDeviation = ( Math.sqrt(gSum / (tab.length - 1)));

        System.out.println("\nYour sets of integers: " + Arrays.toString(tab));
        System.out.println("\nThe largest integers is: " + maxInt);
        System.out.println("\nThe smallest integers is: " + minInt);
        System.out.printf("The mean is: %.1f);\n", mean);
        System.out.printf("The standard deviation is: %.2f", sDeviation);


    }
}

2 个答案:

答案 0 :(得分:1)

在阅读下一行之前使用Scanner#hasNextLine()方法进行检查

 if(in.hasNextLine())
 String numbers = in.nextLine();
如果找不到行,则在调用NoSuchElementException时抛出

in.nextLine()

答案 1 :(得分:1)

您需要进行三项更改(第3步是您的计划在第1步失败的原因) -

<强> 1

// to handle no more input.
String numbers = (in.hasNextLine()) ? in.nextLine() : "";

<强> 2

Scanner in = new Scanner(System.in); // <-- Move to outside your `do` loop.

第3

// in.close(); // This closes System.in.