“线程中的异常”;“主要”;java.util.NoSuchElementException-无关闭()

“线程中的异常”;“主要”;java.util.NoSuchElementException-无关闭(),java,exception,nosuchelementexception,Java,Exception,Nosuchelementexception,我有一个问题需要在接下来的8小时内解决(最多),我读了很多有类似问题的帖子,但他们总是调用remove close()。我没有,我的问题仍然存在 package Kolokwium; import java.io.*; import java.util.Scanner; public class Group{ int availableseats; int occupiedseats= 0; public Group() { try (

我有一个问题需要在接下来的8小时内解决(最多),我读了很多有类似问题的帖子,但他们总是调用remove 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!");}
    }
}

你能把所有变量和方法名翻译成英语吗?像这样的代码几乎不可读。即使你不是以英语为母语的人,也最好只使用英语进行编程。以下几点可以帮助你得到答案:1)正确缩进你的代码,很难读懂它,2)如果您确切地指定异常从何处引发,这将非常有帮助。我不明白为什么
NoTouchElementException
会与
close()
方法有任何关联。在任何情况下,异常的堆栈跟踪都会提供有关该问题的大量信息。@JanHenke我不同意英语是一种良好的行业实践,但我同意它可以让其他人更容易阅读代码……您可以添加堆栈跟踪吗?这通常是非常有价值的信息……这不是问题所在,即使我从程序中删除了pokaz_liste,它也向我显示了同样的问题。我忘了准确地写Eclipse告诉我的内容:java.util.Scanner.throwFor(未知源)java.util.Scanner.next(未知源)java.util.Scanner.nextInt(未知源)java.util.Scanner.nextInt(未知源)Kolokwium.Grupa.main(Grupa.java:76)java.util.Scanner.NoSuchElementException线程“main”中的异常@Galaxis发布堆栈跟踪请更改为“in.nextLine();int-iwybor=in.nextInt();”将我的问题更改为:在java.util.Scanner.nextLine(未知源)@Galaxis删除该行。在
构造函数中,
seats2
在哪里声明?不,我不能。还有,我犯了一个小错误——2号座位应该是可用的座位。如果我把这个放在小组里,我需要把扫描仪的声明也放在这里,那么我不能在主要部分使用“ichoice”。。。
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)
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]);
    }
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();
        }
    }