gpt4 book ai didi

java - 线程 "main"java.util.NoSuchElementException 中的异常 - 无 close()

转载 作者:行者123 更新时间:2023-11-29 04:46:41 26 4
gpt4 key购买 nike

我有这个问题需要在接下来的 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!");}
}
}

当涉及“int ichoice = in.nextInt();”时,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)

最佳答案

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()

看在上帝的份上,冲洗并关闭你的流!

对于您的异常(exception)情况:扫描仪已耗尽。试试这个

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();
}
}

关于java - 线程 "main"java.util.NoSuchElementException 中的异常 - 无 close(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36866416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com