Java 如何让类变量文件检查打印行而不是抛出异常

Java 如何让类变量文件检查打印行而不是抛出异常,java,java-8,Java,Java 8,我试图获得一个方法来检查文件是否存在,并打印一条消息,说明文件不存在,但它也必须是类变量而不是实例变量 当它只在subString方法中工作时,它不是一个类变量,并且没有中缀/后缀/前缀代码 这是我的密码。它仍然有点凌乱,不符合格式化惯例 谢谢你的帮助 package wordgames; import java.io.File; import java.util.Scanner; import java.io.FileInputStream; import java.io.FileReade

我试图获得一个方法来检查文件是否存在,并打印一条消息,说明文件不存在,但它也必须是类变量而不是实例变量

当它只在subString方法中工作时,它不是一个类变量,并且没有中缀/后缀/前缀代码

这是我的密码。它仍然有点凌乱,不符合格式化惯例

谢谢你的帮助

package wordgames;

import java.io.File;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;

public class WordGames {

    private static final String DICTIONARY = "dictionary.txt";
    private static String[] wordsCollection;
    private static int wordCount = 0;

    public static void main(String[] args) throws Exception {
        wordsCollection = new String[100];
        File fileReader = new File(DICTIONARY);
        Scanner fileScanner = new Scanner(fileReader);

        while (fileScanner.hasNextLine()) {
            String line = fileScanner.nextLine();
            wordsCollection[wordCount] = line;
            wordCount++;
        }

        getSelection();
    }

    static String getSelection() throws FileNotFoundException {
        Scanner keyboardInput = new Scanner(System.in);
        System.out.println("Welcome to the Word Games program menu.");
        System.out.println("Select from one of the following options.");
        System.out.println("1. Substring problem.");
        System.out.println("2. Points problem.");
        System.out.println("3. Exit.");
        System.out.println("Enter your selections: ");
        String selection = keyboardInput.next();

        switch (selection) {

        case "1":
            subStringProblem();
        case "2":
            pointsProblem();
        case "3":
            System.out.println("Good Bye!");
            System.exit(0);
        default:
            System.out.println("Invalid option.  Try again.");
            getSelection();
        }

        return null;
    }

    static void subStringProblem() throws FileNotFoundException {
        File fileReader = new File("DICTIONARY.txt");
        Scanner fileScanner = new Scanner(fileReader);
        if (fileReader.isFile() == true) {
        } else {
            System.out.println("File doesn't exist.  Exiting.");
            System.exit(0);
        }
        System.out.println("Substring problem.");
        System.out.println("Enter a Substring:");
        Scanner keyboardInput = new Scanner(System.in);
        String subString = keyboardInput.next();
        System.out.print(subString);
        System.out.println();
        String notFound = " - not found";
        String infixFound = " - infix";
        String prefixFound = " - prefix";
        String suffixFound = " - suffix";

        for (int i = 0; i < wordCount; i++) {
            String temp = wordsCollection[i];

            boolean found = false;

            if (wordsCollection[i].startsWith(subString)) {
                found = true;
                temp = temp + prefixFound;
            }

            if (wordsCollection[i].endsWith(subString)) {
                found = true;
                temp = temp + suffixFound;
            }

            if (wordsCollection[i].contains(subString)) {
                found = true;
                temp = temp + infixFound;
            }

            if (!found) {
                System.out.printf(" " + wordsCollection[i] + notFound + "\n");
            } else {
                System.out.printf(" " + temp + "\n");
            }

        }

        getSelection();
    }

    private static void pointsProblem() throws FileNotFoundException {
        System.out.println("Points problem.");
        getSelection();
    }
}
    
套装文字游戏;
导入java.io.File;
导入java.util.Scanner;
导入java.io.FileInputStream;
导入java.io.FileReader;
导入java.io.FileNotFoundException;
导入java.util.InputMismatchException;
公开课文字游戏{
私有静态最终字符串DICTIONARY=“DICTIONARY.txt”;
私有静态字符串[]字集合;
私有静态int字数=0;
公共静态void main(字符串[]args)引发异常{
wordsCollection=新字符串[100];
文件读取器=新文件(字典);
Scanner fileScanner=新扫描仪(文件阅读器);
while(fileScanner.hasNextLine()){
String line=fileScanner.nextLine();
wordsCollection[wordCount]=行;
字数++;
}
getSelection();
}
静态字符串getSelection()引发FileNotFoundException{
扫描仪键盘输入=新扫描仪(System.in);
System.out.println(“欢迎来到文字游戏程序菜单”);
System.out.println(“从以下选项中选择”);
System.out.println(“1.子字符串问题”);
System.out.println(“2.点问题”);
System.out.println(“3.Exit”);
System.out.println(“输入您的选择:”);
字符串选择=键盘输入。下一步();
开关(选择){
案例“1”:
子串问题();
案例“2”:
pointsProblem();
案例“3”:
System.out.println(“再见!”);
系统出口(0);
违约:
System.out.println(“无效选项。请重试”);
getSelection();
}
返回null;
}
静态void subStringProblem()引发FileNotFoundException{
File fileReader=新文件(“DICTIONARY.txt”);
Scanner fileScanner=新扫描仪(文件阅读器);
if(fileReader.isFile()==true){
}否则{
System.out.println(“文件不存在。正在退出”);
系统出口(0);
}
System.out.println(“子字符串问题”);
System.out.println(“输入子字符串:”);
扫描仪键盘输入=新扫描仪(System.in);
String subString=keyboardInput.next();
系统输出打印(子字符串);
System.out.println();
字符串notFound=“-notFound”;
字符串infixFound=“-infix”;
字符串prefixFound=“-prefix”;
字符串SUFFEXFOUND=“-suffix”;
for(int i=0;i
我注意到您在所有函数的签名中都添加了
抛出FileNotFoundException
。只有当您不想捕获该函数中的异常,并且希望调用方负责处理该异常时,才可以这样做。在您的程序中,情况从来不是这样,您总是希望通过退出立即处理它,所以将其从任何地方删除

正在崩溃的代码部分:

File fileReader=新文件(“DICTIONARY.txt”);
Scanner fileScanner=新扫描仪(文件阅读器);
if(fileReader.isFile()==true){
否则{
System.out.println(“文件不存在。正在退出”);
系统出口(0);
}
如果删除
fileScanner
,此代码将起作用。如果文件不存在,则会引发异常,但由于您实际上没有将其用于任何用途,因此可以将其删除。
fileReader.isFile()
检查将完成此工作。(
fileReader.exists()
更接近)

更正后的代码:

File fileReader=新文件(“DICTIONARY.txt”);
if(fileReader.exists()==false){
System.out.println(“文件不存在。正在退出”);
系统出口(0);
}
您还在
main()
函数中读取一个文件。实际上您正在使用
fileScanner
,因此您可以选择使用与上面相同的检查,或者您可以实际捕获
FileNotFoundException
,这一次,您至少可以尝试一下

试试看{
文件读取器=新文件(字典);
Scanner fileScanner=新扫描仪(文件阅读器);
while(fileScanner.hasNextLine()){
String line=fileScanner.nextLine();
wordsCollection[wordCount]=行;
字数++;
}
}catch(filenotfounde异常){
System.out.println(“文件不存在。正在退出”);
系统出口(0);
}

如果您在try/catch块中实际调用了一个函数,这将使它看起来不那么丑陋,这将是一个很好的做法。

这是否有助于使
fileScanner
成为