Quantcast
Channel: How to loop a try catch statement? - Stack Overflow
Viewing all articles
Browse latest Browse all 6

How to loop a try catch statement?

$
0
0

How do you loop a try/catch statement? I'm making a program that is reading in a file using a Scanner and it's reading it from the keyboard. So what I want is if the file does not exist, the program will say "This file does not exist please try again." then have the user type in a different file name. I have tried a couple different ways to try an do this but, all of my attempts end up with the program crashing.

Here is what I have

    try {
        System.out.println("Please enter the name of the file: ");
        Scanner in = new Scanner(System.in);
        File file = new File(in.next());
        Scanner scan =  new Scanner(file);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("File does not exist please try again. ");
    }

Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images