繁体   English   中英

如果我要求用户输入一个文件,而该文件不存在,如何在程序不停止的情况下继续询问文件名?

[英]If I am asking a user to input a file, and the file does not exist, how can I continue to ask for the file name without the program stopping?

这就是我所拥有的......我知道它应该需要一个循环,或者可能需要文件 class 中的.empty() 方法,但我不确定.. 任何帮助表示赞赏。 我所拥有的将打开一个文件,从文件中读取每一行,然后返回文件中的字符数、文件中的单词数以及文件中的句子数。

public class FileExample{
    public static void main(String[] args) throws FileNotFoundException, IOException{
        Scanner in = new Scanner(System.in);
        boolean fileFound = false;
        try{
            System.out.println("What is the name of the file?");
            inputFile = in.nextLine();
            File file = new File(inputFile);
            fileFound = file.exists();
            FileInputStream fileStream = new FileInputStream(file); 
            InputStreamReader input = new InputStreamReader(fileStream);
            BufferedReader reader = new BufferedReader(input);
            if(!file.exists()){

            }
            while((line = reader.readLine()) != null){
                if(!(line.equals(""))){
                    ...
                }
            }
        }
        catch(FileNotFoundException e){
            System.out.println("File not found.");
        }
        System.out.println("output data");
    }   
}

您需要创建一个 while 循环并将 try 块移动到循环内。

while(true){
    try{
        System.out.println("What is the name of the file?");
        inputFile = in.nextLine();
        File file = new File(inputFile);
        if(!file.exists()){
            continue;
        }
        FileInputStream fileStream = new FileInputStream(file); 
        InputStreamReader input = new InputStreamReader(fileStream);
        BufferedReader reader = new BufferedReader(input);
        while((line = reader.readLine()) != null){
            if(!(line.equals(""))){
                characterCount += line.length();
                String[] wordList = line.split("\\s+");
                countWord += wordList.length;
                String[] sentenseList = line.split("[!?.:]+");
                sentenseCount += sentenseList.length;
            }
        }
        break;
    }
    catch(FileNotFoundException e){
        System.out.println("File not found.");
    }
}

使用 while 循环,直到文件存在。 例子:

...
System.out.println("What is the name of the file?");
inputFile = in.nextLine();
File file = new File(inputFile);
fileFound = file.exists();
while(!fileFound){
    System.out.println("The file does not exist. What is the name of the file?");
    inputFile = in.nextLine();
    file = new File(inputFile);
    fileFound = file.exists();
}
FileInputStream fileStream = new FileInputStream(file); 
InputStreamReader input = new InputStreamReader(fileStream);
BufferedReader reader = new BufferedReader(input);
...

在您的代码中添加一个 do-while。 像这样。 不完全确定这是否会运行,但我希望你能明白

do {
    try {
        System.out.println("What is the name of the file?");
        inputFile = in.nextLine();
        File file = new File(inputFile);
        fileFound = file.exists();
        FileInputStream fileStream = new FileInputStream(file); 
        InputStreamReader input = new InputStreamReader(fileStream);
        BufferedReader reader = new BufferedReader(input);

        while((line = reader.readLine()) != null){
            if(!(line.equals(""))){
                characterCount += line.length();
                String[] wordList = line.split("\\s+");
                countWord += wordList.length;
                String[] sentenseList = line.split("[!?.:]+");
                sentenseCount += sentenseList.length;
            }
        }
    } catch (FileNotFoundException e){
        System.out.println("File not found.");
    }
} while (!fileFound) {
    System.out.println("Character count: "+characterCount);
    System.out.println("Word count: "+countWord);
    System.out.println("Sentence count: "+sentenseCount);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM