简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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