简体   繁体   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?

This is what I have... I know it should require a loop or perhaps the.empty() method from the File class, but I'm not sure.. any help is appreciated.这就是我所拥有的......我知道它应该需要一个循环,或者可能需要文件 class 中的.empty() 方法,但我不确定.. 任何帮助表示赞赏。 What I have will open a file, read from the file on each line, and then return back the amount of characters in the file, the amount of words in the file, and the number of sentences in the file.我所拥有的将打开一个文件,从文件中读取每一行,然后返回文件中的字符数、文件中的单词数以及文件中的句子数。

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");
    }   
}

You need to make a while loop and move the try block inside the loop.您需要创建一个 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.");
    }
}

Use a while loop, until the file exists.使用 while 循环,直到文件存在。 Example:例子:

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

Add a do-while to your code.在您的代码中添加一个 do-while。 Like this.像这样。 not exactly sure if that's gonna run but i hope you get the idea不完全确定这是否会运行,但我希望你能明白

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.

相关问题 我希望让用户输入 .txt 文件名,如果文件名不存在,则重新提示用户。 这怎么可能? - I am wishing to ask the user to input a .txt file name, and re-prompt the user if the file name does not exist. How is this possible? 如何询问用户输入的文件名,然后使用该文件名? - How do I ask a user for a input file name, and then use that file name? 请求用户输入后如何使我的程序继续运行? - How can I make my program continue after asking for user input? 如何询问用户输入文件名和输出文件名java? - How to ask user for an input file name and output file name java? 我的 do while 循环询问用户新文件时遇到问题是前一个不起作用 - I am having trouble with my do while loop for asking a user for a new file is the previous one does not work 如果文件名已经存在,如何让用户再次输入文本文件名? - How can I let user input the name of text file again if the name of file already exists? 如果File不是文件或目录,如何找出它? - How can I find out if a File is a file or directory if it does not exist? Java - 如何继续询问用户输入? - Java - How do I continue to ask the user for input? 我是编码新手。 如果用户输入错误,我们如何在不退出代码的情况下要求重新输入? - I am new to coding. if user give wrong input how can we ask to reenter input without exiting code? 如何要求用户输入和输出文件名? - how to ask user for output and input file names?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM