Why is this code getting a Java NoSuchElement exce

2019-01-28 11:59发布

I have traced through this code and can't figure out how to fix it. When running the code why wouldn't the user be prompted for input rather than Java determining that there is no input? Error trace below.

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;

        }

Here is the error --

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)

3条回答
时光不老,我们不散
2楼-- · 2019-01-28 12:22

Removing the scanner.close() method from two functions will solve your problem.

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;

        }
 }
查看更多
干净又极端
3楼-- · 2019-01-28 12:25

The simple answer is when you close Scanner -- underlying input stream also closes: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#close()

To fix this create Scanner once in main:

 public class SortAsInserted {
     static Scanner input;
     public static void main(String[] _) {
         input = new Scanner(System.in); 
         ....
         input.close();  
     }
查看更多
你好瞎i
4楼-- · 2019-01-28 12:33

Remove input.close(); from the code

查看更多
登录 后发表回答