簡體   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