Exception in thread "main" java.lang.IllegalStateException: Scanner closed at 1070,1334. Java returned 1
此代码从项目文件夹中的Course.txt
文件中读取,并且在开始阅读文本之前(在完成第一行之前),它具有此异常错误。我迷路了,因为这是我第一次为此创建Java程序。
我在netbeans上没有检查过两次错误,也没有尝试,并且尝试删除和替换代码的某些区域也没有成功。
package u2a1_readtextfilehandleexcep;
import java.util.Scanner;
import java.io.IOException;
//@author jeff thurston
public class U2A1_ReadTextFileHandleExcep {
public static void main(String[] args) throws Exception {
// TODO code application logic here
System.out.println("U2A1 Java Read Text File");
try
{
java.io.File file1 = new java.io.File("Course.txt");
Scanner input = new Scanner(file1);
while (input.hasNext())
{
String coursecode = input.next();
int coursehours = input.nextInt();
String coursetitle = input.next();
System.out.println ("Course code = " + coursecode + " -
Credit Hours = " + coursehours + " - " + "Course Title = " +
coursetitle);
input.close();
}
}
catch (IOException e)
{
System.out.println ("Error in reading the file" +
e.getMessage());
}
}
}
它应如下运行:
U2A1 Java Read Text File
Course code = IT2249 - Credit Hours = 6 - Course Title = Introduction to Programming with Java
Course code = IT2230 - Credit Hours = 3 - Course Title = Introduction to Database Systems
Course code = IT4789 - Credit Hours = 3 - Course Title = Mobile Cloud Computing Application Development
但是第一行Course Title = Introduction
之后是系统停止产生输出的位置。