所以我有3个不同的类,每个都有一个扫描仪。在main方法中,我循环遍历这些类,并为每个类输入几次。问题是当我关闭扫描仪(scanner.close();)时,我只能迭代一次,执行的第一个类,然后我得到这个错误:线程“main”中的异常java.util.NoSuchElementException:找不到行。当我不关闭扫描仪时,一切都很好,但我不想有内存泄漏。如果发布代码可以帮助您更好地理解我的问题,请让我知道
''公共类主{
public static void main(String args[]){
Employee new_guard[] = new Guard[5];
Employee new_it[] = new ITPersonal[5];
Employee new_manager[] = new Management[5];
int count_guard = 0, count_it = 0, count_manager = 0;
for(int i = 0; i <= 14; i++){
if(i % 3 == 0){
new_guard[count_guard] = new Guard();
new_guard[count_guard].input_variables();
count_guard++;
System.out.println();
}else if(i % 2 == 0){
new_it[count_it] = new ITPersonal();
new_it[count_it].input_variables();
count_it++;
System.out.println();
}else{
new_manager[count_manager] = new Management();
new_manager[count_manager].input_variables();
count_manager++;
System.out.println();
}
}
int k = 0, j = 0, l = 0;
for(int i = 0; i < 15; i++){
if(i % 3 == 0){
System.out.println(new_manager[k].toString());
k++;
}else if(i % 2 == 0){
System.out.println(new_it[j].toString());
j++;
}else{
System.out.println(new_guard[l].toString());
l++;
}
}
}
}
抽象公共类员工{
public Employee(){}
String name, surname, rang;
int salary = 0, daysoff = 20;
public Employee(String name, String surname, String rank, int salary, int daysoff){
this.name = name;
this.surname = surname;
this.salary = salary;
this.rank = rank;
this.daysoff= daysoff;
}
}
导入java.util.扫描仪;
公共类守卫扩展员工{
String rang = "Guard";
int night_shifts, early_shifts, late_shifts;
public Guard(){}
public void input_variables(){
Scanner scan = new Scanner(System.in);
System.out.println("First name: ");
this.name = scan.nextLine();
System.out.println("Last name: ");
this.surname = scan.nextLine();
System.out.println("Night shifts (per month): ");
this.night_shifts = scan.nextInt();
System.out.println("Late shifts (per month): ");
this.late_shifts = scan.nextInt();
System.out.println("Night shifts (per month): ");
this.early_shifts = scan.nextInt();
// scan.close();
System.out.printf("Added %s, %s to company!", this.surname, this.name);
}
public int getSalary(){
return salary = 160 * night_shifts + 100 * (early_shifts + late_shifts);
}
public int getDaysOff(){
return daysoff += 0.25 * (early_shifts + late_shifts + night_shifts);
}
public String toString(){
return String.format("%s, %s (%s): Shifts %d/%d/%d; Monthly salary = %d EURO; %d offdays.",
surname, name, rank, early_shifts, late_shifts, night_shifts, getSalary(), getDaysOff());
}
}
导入java.util.扫描仪;
公共类ITPersonal扩展员工{
String rang;
int year_joined, weekhours, daysoff = 22;
int current_year = 2021, bonusdays = 0;
public ITPersonal(){}
public void input_variables(){
Scanner scan = new Scanner(System.in);
System.out.println("First name: ");
this.name = scan.nextLine();
System.out.println("Last name: ");
this.surname = scan.nextLine();
System.out.println("Enter rank (\"Junior Developer" + " or " + "Senior Developer\"): \"");
this.rank = scan.nextLine();
System.out.println("Year joined: ");
this.year_joined = scan.nextInt();
System.out.println("Week hours: ");
this.weekhours = scan.nextInt();
// scan.close();
System.out.printf("Added %s, %s to company!", this.surname, this.name);
}
public int getDaysOff(){
if(current_year - year_joined >= 3){
if((current_year - year_joined) % 3 == 0){
if((current_year - year_joined) / 3 >= 5){
bonusDays = 5;
}else
bonusDays = (current_year - year_joined) / 3;
}else{
if((current_year - year_joined) / 3 >= 5){
bonusDays = 5;
}else
bonusDays = (current_year - year_joined) % 3;
}
}
if(this.rang == "Senior Developer"){
daysoff += bonusTage;
}else
daysoff = 22;
return daysoff;
}
public int getSalary(){
if(rang == "Junior Developer"){
salary = 25 * weekhours;
}else{
salary = (32 * weekhours) + current_year - year_joined;
}
return salary;
}
public String toString(){
return String.format("%s, %s (%s): Weekhours = %d; Monthly salary = %d EURO; %d offdays.",
this.surname, this.name, this.rank, this.weekhours, getSalary(), getDaysOff());
}
}
“”
扫描仪不是问题所在。初始化扫描仪时,您可能正在执行
Scanner scanner = new Scanner(System.in);
问题是调用扫描器时。close(),扫描仪正在关闭系统。在
中,它是进程的单例(从控制台读取的流)。用正则类包装单例(系统中的)并不是隐藏其存在的解决方案。
要解决此问题,您只需初始化扫描仪一次,然后在程序结束时将其关闭(仅一次)。然后通过依赖项注入给类Scanner的引用。
这方面的一个很好的实现是使用try with resource:
public class Main {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
// the rest of your program
} // scanner is automatically closed after the try block.
}
}
肯定有一条线,但我不明白为什么扫描仪看不到… 以下是文件的开头: 下面是我获取它的代码: 但我得到了错误: book1_enc的文件是我的LZW编码算法的输出。当我将文件传递给我的解码器时,我希望解码器知道字典的大小,在这种情况下是256...感谢您的阅读...
我刚开始我的大学java课程,在扫描器类中不断得到这个错误。 我不断得到的错误是:
问题内容: 我收到以下异常。 java.util.NoSuchElementException:找不到行 我在编写一个需要从文本文件读取的较大程序时遇到了此错误,因此决定进行测试。 而且我仍然得到例外。我在与名为stricts.txt的类相同的文件夹中有一个文本文件,其中包含文本。我究竟做错了什么? 问题答案: 新的File(“ restrictions.txt”)将在应用程序的“开始目录”中查找
我知道有很多关于这个例外的帖子,但是我不能解决这个问题。我认为必须编辑类路径来解决它。我试图在hadoop架构体系中运行一个名为远程地图的程序。这是我得到的错误。 Echo$CLASSPATH给出了一个空行 cat~/。bash_简介说 更新: $HADOOP_HOME $HADOOP_CLASSPATH 有人能帮我解决这个问题吗? 谢谢
jar-cvfe-dhj。jar DefaultHadoopJobDriver 我的Hadoop作业只有一个类“DefaultHoopJobDrive”,它扩展了配置和实现工具,并将方法作为作业创建和inputpath、outpurpath集的唯一代码运行。我也在使用新的API。
我在线程“main”java中遇到异常。错误:未解析编译。 我做错了什么? 线程“main”中出现异常: java.lang.错误:未解决的编译问题: 对于类型在