Reputation: 640
I am trying to play around with opening, closing, and writing to files. Whenever I try to open a file, if it doesn't exist at the path I provide the program tells me. If it exists, the program reads whatever is inside and displays it. If the user doesn't want to look for a file, there is an option to create a file and fill it with data.
It works so far except whenever I read a file, after it is successful in displaying the data I get an unknown source Scanner exception. Here is my code so far:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class FileIO {
public static void main(String[] args) throws IOException{
Scanner input = new Scanner(System.in);
while (true){
System.out.println("1. Load file from path of your choosing.");
System.out.println("2. Answer eight questions and save the answers into a file.");
System.out.println("3. Exit the program.");
int userChoice = input.nextInt();
switch (userChoice){
case 1:
loadFile();
break;
case 2:
answerQuestions();
break;
case 3:
System.out.println("Goodbye!");
break;
}
}
}
public static void loadFile() throws IOException{
System.out.println("What is the file name you want to try to read?");
Scanner input = new Scanner(System.in);
File f = new File(input.nextLine());
if (f.exists() && !f.isDirectory()){
System.out.println("The file exists!");
Scanner inputFile = new Scanner(f);
while (inputFile.hasNext()){
String answer = inputFile.next();
System.out.println(answer);
}
input.close();
}
else {
System.out.println("File does not exist at that given path!");
}
}
public static void answerQuestions() throws IOException{
Scanner input = new Scanner(System.in);
System.out.println("What is your name?"); // 1
String name = input.nextLine();
System.out.println("What is your favorite color?"); // 2
String color = input.nextLine();
System.out.println("What is your favorite type of music?"); // 3
String music = input.nextLine();
System.out.println("What is your favorite place?"); // 4
String place = input.nextLine();
System.out.println("What is your favorite food?"); // 5
String food = input.nextLine();
System.out.println("What is your favorite book?"); // 6
String book = input.nextLine();
System.out.println("What is your favorite programming language?"); // 7
String language = input.nextLine();
System.out.println("Do you prefer laptop or desktop computers?"); // 8
String computer = input.nextLine();
System.out.println("All the questions are answered. Name the file you want to save the answers to: ");
File f = new File(input.nextLine());
if (f.exists()){
System.out.println("File already exists!");
return;
}
PrintWriter output = new PrintWriter(f);
output.print(name + "\n");
output.print(color + "\n");
output.print(music + "\n");
output.print(place + "\n");
output.print(food + "\n");
output.print(book + "\n");
output.print(language + "\n");
output.print(computer + "\n");
output.close();
}
}
This is the error:
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 Lab4.main(Lab4.java:18)
What exactly is the problem? I tried changing the Scanner input on line 19 to input2 but that didn't work. The program lets me select as many menu options as I want if the file does not exist when checking a path, so I'm guessing something is wrong with the program at the part where it starts printing stuff to the console if a file is found. But I have no idea what it could be. I would appreciate any tips or hints that guide me towards handling this error. Thanks for your time.
Upvotes: 0
Views: 1133
Reputation: 2411
Remove input.close();
from loadFile()
function as it is closing referenced standard input stream blocking further IO.
Upvotes: 0
Reputation: 58858
Scanner input = new Scanner(System.in);
/* other code here */
input.close();
Closing a Scanner also closes the stream it's using. You closed System.in
. Just like any other stream, if it's closed, you can't use it.
The ideal solution would be to create a Scanner once (at the start of the program) and then use it every time you want to read input.
Upvotes: 1