线程" main"中的例外情况java.util.NoSuchElementException - no close()

时间:2016-04-26 13:29:01

标签: java exception nosuchelementexception

我有这个问题我需要在接下来的8小时内解决(最多),我读了很多类似问题的帖子,但是他们总是调用删除close()。我没有,我的问题仍然存在。

package Kolokwium;
import java.io.*;
import java.util.Scanner;

public class Group{
    int availableseats;
    int occupiedseats= 0;

    public Group()  {
        try (
               PrintWriter writer = new PrintWriter("C:\\Users\\Galaxis\\Desktop\\lesson_name.txt");
               BufferedReader reader  = new BufferedReader(new InputStreamReader(System.in));
            ) {
                System.out.println("Lesson name: ");
                String lesson_nameu = reader.readLine();
                System.out.println("Available seats:");
                String seats= reader.readLine();
                writer.println(lesson_name + " " + seats);
                availableseats= Integer.parseInt(seats);
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void add_student() {
        if (occupiedseats < availableseats) {
            try (
              PrintWriter writer = new PrintWriter("C:\\Users\\Galaxis\\Desktop\\lesson_name.txt");
              BufferedReader reader  = new BufferedReader(new InputStreamReader(System.in));
            ) {
                System.out.println("Write student's data: ");
                String data = reader.readLine();
                occupiedseats += 1;
                writer.println(data);
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }
        } else {
            System.out.println("No available seats!");
        }
    }

    public void show_list() {
        File path = new File("C:\\Users\\Galaxis\\Desktop\\lesson_name.txt");
        String[] list;
        list = path.list();
        for (int i=0; i < list.length; i++)
        System.out.println(list[i]);
    }

    public static void main(String[] args) {

        Group group = new Group();
        Scanner in = new Scanner(System.in);
        System.out.println("MENU " + "1. Add student. " + "2. Show list. ");
        int ichoice = in.nextInt();

        if(ichoice  == 1) {
            group.add_student();
        }
        else if(ichoice == 2) {
            group.show_list();
        }
        else {System.out.println("Wrong choice!");}
    }
}

当涉及到&#34; int ichoice = in.nextInt();&#34;

时,Eclipse会给我这个消息。
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 Kolokwium.Grupa.main(Grupa.java:76)

1 个答案:

答案 0 :(得分:3)

public void pokaz_liste() {
        File path = new File("C:\\Users\\Galaxis\\Desktop\\nazwa_przedmiotu.txt");
        String[] list;
        list = path.list();
        for (int i=0; i < list.length; i++)
        System.out.println(list[i]);
    }

您正在尝试从文件中获取文件列表。 Javadoc声明,如果文件实例没有指向目录,它将返回null。

https://docs.oracle.com/javase/7/docs/api/java/io/File.html#list()

为了上帝的缘故,冲洗并关闭你的溪流!

您的例外情况:扫描仪已耗尽。试试这个

public Group()  {
        try (
               PrintWriter writer = new PrintWriter("C:\\Users\\Galaxis\\Desktop\\lesson_name.txt");
            ) {
                BufferedReader reader  = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Lesson name: ");
                String lesson_nameu = reader.readLine();
                System.out.println("Available seats:");
                String seats= reader.readLine();
                writer.println(lesson_name + " " + seats);
                seats2 = Integer.parseInt(seats);
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }