为什么此代码会出现 Java NoSuchElement 异常?

Why is this code getting a Java NoSuchElement exception?

我已经跟踪了这段代码,但不知道如何修复它。当 运行 代码为什么不提示用户输入而不是 Java 确定没有输入?错误跟踪如下。

import java.util.*;
public class SortAsInserted {

    public static void main(String[] args) {
        int array_size = GetArraySize();
        //System.out.println(array_size);
        String[] myArray = new String[array_size];
        for (int i = 0; i < array_size; i++){
            String next_string = GetNextString();
            System.out.println(next_string);
        }
    }



    //public static String[] SortInsert(String nextString){
        //}

    public static int GetArraySize(){
        Scanner input = new Scanner(System.in);
        System.out.print("How many items are you entering?: ");
        int items_in_array = input.nextInt();
        input.close();
        return items_in_array;


    }

    public static void PrintArray(String[] x) {
        for (int i = 0; i < x.length; i++){
            System.out.print(x[i]);
        }

    }

    public static String GetNextString(){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the next string: ");
        String next_string = input.nextLine();
        input.close();
        return next_string;

        }

这里是错误--

How many items are you entering?: 2
Enter the next string: 
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at SortAsInserted.GetNextString(SortAsInserted.java:40)
    at SortAsInserted.main(SortAsInserted.java:10)

从代码中删除 input.close();

从两个函数中删除 scanner.close() 方法将解决您的问题。

import java.util.*;
public class SortAsInserted {

    public static void main(String[] args) {
        int array_size = GetArraySize();
        //System.out.println(array_size);
        String[] myArray = new String[array_size];
        for (int i = 0; i < array_size; i++){
            String next_string = GetNextString();
            System.out.println(next_string);
        }
    }



    //public static String[] SortInsert(String nextString){
        //}

    public static int GetArraySize(){
        Scanner input = new Scanner(System.in);
        System.out.print("How many items are you entering?: ");
        int items_in_array = input.nextInt();
        //input.close();
        return items_in_array;


    }

    public static void PrintArray(String[] x) {
        for (int i = 0; i < x.length; i++){
            System.out.print(x[i]);
        }

    }

    public static String GetNextString(){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the next string: ");
        String next_string = input.nextLine();
       // input.close();
        return next_string;

        }
 }

简单的答案是当您关闭 Scanner 时——底层输入流也会关闭: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#close()

要修复此问题,请在 main 中创建一次扫描程序:

 public class SortAsInserted {
     static Scanner input;
     public static void main(String[] _) {
         input = new Scanner(System.in); 
         ....
         input.close();  
     }