文件I / O期间来自未知来源的Java Scanner异常

时间:2014-07-26 08:17:09

标签: java exception

我正在尝试打开,关闭和写入文件。每当我尝试打开文件时,如果它在我提供的路径中不存在,程序会告诉我。如果它存在,程序将读取内部的任何内容并显示它。如果用户不想查找文件,则可以选择创建文件并用数据填充。

它的工作原理到目前为止除了每当我读取文件时,在成功显示数据后,我得到一个未知的源扫描程序异常。到目前为止,这是我的代码:

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

    }
}

这是错误:

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)

究竟是什么问题?我尝试将第19行的扫描仪输入更改为input2,但这不起作用。该程序允许我选择尽可能多的菜单选项,如果在检查路径时文件不存在,所以我猜测程序在开始将内容打印到控制台的部分有问题(如果找到文件) 。但我不知道它可能是什么。我将不胜感激任何指导我处理此错误的提示或提示。谢谢你的时间。

2 个答案:

答案 0 :(得分:1)

Scanner input = new Scanner(System.in);
/* other code here */
input.close();

关闭扫描仪也会关闭它使用的流。您已关闭System.in。就像任何其他流一样,如果关闭,您就无法使用它。

理想的解决方案是创建一次扫描程序(在程序开始时),然后在每次想要读取输入时使用它。

答案 1 :(得分:0)

input.close();函数中删除loadFile(),因为它关闭了引用的标准输入流,阻止了进一步的IO。