尝试处理catch块时发生错误

时间:2019-02-07 01:59:47

标签: java debugging javahelp

现在,我正在编写一个程序,该程序接受用户输入(即配方),并从另一个文件中读取配方,然后将配方输出到名为“ ShoppingList.txt”的新文件中。我试图创建文件,但是当我尝试编译程序时,出现此错误。  RecipeList.java:80:错误:异常FileNotFoundException从未抛出在相应try语句的主体中                 catch(FileNotFoundException e)有人可以帮我调试它。谢谢!

//Prasanth Dendukuri
// 2/5/18
// RecipeList.java


import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.IOException;

public class RecipeList
{
    private Scanner terminal; // to read in input from kb
    private Scanner reader; // to read in from the text fi;e
    private String inFileName; // name of file to read from
    private String outFileName; // name of file to output to
    private String[] recipeList; // array that contains the recipes
    private String[] ingredients; // array that contains ingredients for each recipe
    private PrintWriter output; // print writer for writing to output file
    private String input; // to accept input and store in this variable
    private int numInput; // num of recipes entered
    private Scanner file; // scanner used for reading in from file

    public RecipeList()
    {
        terminal = new Scanner(System.in);
        inFileName = new String("Recipes.txt");
        outFileName =  new String("ShoppingList.txt");
        recipeList =  new String[20];
        ingredients = new String[20];
        input = new String(" ");
        output = null;
        numInput = 0;

    }

    public static void main(String[] args)
    {
        RecipeList rl = new RecipeList();
        rl.run();
    }

    // calls all methods and prints blank lines
    public void run() 
    {
        System.out.print("\n\n\n");
        getInput();
        openRecipes();
        makeFile();
        readFile();
        output.close();
    }

    // gets input of recipes until 'quit' is entered
    public void getInput()
    {
        while(!input.equalsIgnoreCase("quit"))
        {
            System.out.print("Type in a recipe.Type quit to end the program--> ");
            input = terminal.nextLine();

            if(!input.equalsIgnoreCase("quit"))
            {
                recipeList[numInput] = input;
                numInput++;
            }
        }

    }

    //opens RecipeList and handles exceptions 
    public void openRecipes()
    {
        File inFile = new File(inFileName);
        try
        {
            file = new Scanner(inFileName);
        }
        catch(FileNotFoundException e)
        {
            System.err.println("ERROR. Could not read/find file.");
            System.exit(1);
        }
    }

    // makes ShoppingList.txt and handles exceptions 
    public void makeFile()
    {
        File outFile = new File(outFileName);

        try
        {
            output = new PrintWriter(outFile);
        }
        catch(IOException e)
        {
            System.err.println("ERROR. Could not read/find file.");
            System.exit(2);
        }
    }

    public void readFile()
    {
        String directions = new String("");

        while(file.hasNext())
        {
            directions = file.next();
        }

        output.print(directions);

    }

}

            ^

2 个答案:

答案 0 :(得分:0)

使用此代码,请参阅我的评论

    File inFile = new File(inFileName);
    try
    {
        file = new Scanner(inFileName);  
        // you are trying to use the Scanner using a `String`
        // this does not throw an Exception, hence the compile error
        // I think you want to use
        // public Scanner(File source) which throws a FileNotFoundException
        // so change to use instead
        file = new Scanner(inFile);  
    }
    catch(FileNotFoundException e)
    {
        System.err.println("ERROR. Could not read/find file.");
        System.exit(1);
    }

答案 1 :(得分:0)

问题在于openRecipes()方法。您正在尝试处理FileNotFoundException,而扫描程序类未抛出该异常。使用您在扫描程序类中创建的文件对象,它将开始工作。复制粘贴以下代码。

public void openRecipes()
    {
        File inFile = new File(inFileName);
        try
        {
            file = new Scanner(inFile);
        }
        catch(FileNotFoundException e)
        {
            System.err.println("ERROR. Could not read/find file.");
            System.exit(1);
        }
    }