我一直在" java.lang.IllegalStateException"在使用Scanner()
之后这就是我所拥有的
public void insertBook(){ //this method allows the user to insert a new book in the database
Boolean select = false;
Scanner input = new Scanner(System.in);
File textFile = new File("src/newbooks.txt");
FileWriter fw = null; //FileWriter and BufferedWriter have to be initialized and used inside a try catch
//to be used outside, initialize the objects as null
BufferedWriter bw = null;
try{
fw = new FileWriter(textFile);//check if it can read the textfile
bw = new BufferedWriter(fw);//FileWriter has to be wrapped in BufferedWriter
}catch(IOException e){
System.out.println("the file could not be found");
}//end catch
System.out.println(" select is " +select);//if(text == "n")
System.out.println("enter the book information below");
System.out.println("if the informatin is not available, enter\"information not available\"");
System.out.print("do you wnat to enter a book? ");
String text = input.next();
if(text.equals("n"))
select = true;
do{
String book = new String();
if(select == true)
break;
if(text.equals("n")){
System.out.print("inside if no\n");
select = true;
//break;//break here so
}
else if (text.equals("y")){
System.out.println("inside if y");
try{
Scanner input2 = new Scanner(System.in);
System.out.print("enter the title of the book: ");
book = input2.next();
bw.write(book + ", ");
System.out.println("after first write");
System.out.print("enter the author: ");
book = input2.next();
bw.write(book + ", ");
System.out.println();
System.out.print("enter the year of the book: ");
book = input2.next();
bw.write(book + ", ");
System.out.println();
System.out.print("enter the gender: ");
book = input2.next();
bw.write(book + ", ");
//System.out.println();
System.out.print("enter a description: ");
book = input2.next();
bw.write(book + ", ");
System.out.println();
bw.write("\n");
input2.close();
select = false;
}
catch(IOException e){
System.out.println("the text could not be read");
}
input.close();
}
else{
System.out.println("you didn't enter a valid selection");
}
System.out.print("do you want to enter another booK? Enter y for yes and n for no");
text = input.nextLine(); //error here
if(text.equals("n"))
select = true;
}while(select == false);//end while select
//input.close();
}//end of insert method
每当我尝试在if语句之外使用Scanner时,它都会遇到相同的错误。如果我输入了错误的选择或" n",它会起作用,但如果我选择" y"并输入扫描程序到达do-while循环内输入的最后一行后失败的数据。
答案 0 :(得分:3)
...
bw.write("\n");
input2.close();
select = false;
}
catch(IOException e){
System.out.println("the text could not be read");
}
您在此处关闭扫描仪:
input.close(); // <- remove this line
}
else{
System.out.println("you didn't enter a valid selection");
}
...
这里已经关闭了。
...
text = input.nextLine();
if(text.equals("n"))
select = true;
...
上次需要后关闭扫描仪。
这是因为您无法从关闭的扫描仪请求新行。
在有关扫描仪的API文档中了解有关它的更多信息:
public void close()
关闭此扫描仪。如果这个扫描仪还没有 如果它的底层可读也实现了 可关闭的接口然后将调用可读的close方法。 如果此扫描仪已关闭,则调用此方法将具有 没有效果。
尝试在扫描程序后执行搜索操作 关闭将导致IllegalStateException。
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#close()