I'm creating a program that opens and then reads files which users have specified, currently the code I have looks like:
System.out.println("Enter the name of the file you want to open: ");
FileN = scan.nextLine();
// I want the program to return to this point here if an error has occured.
try
{
scan = new Scanner(new File (FileN));
}
catch(Exception e)
{
System.out.println("Could not find file" + e);
System.out.println("Please enter a valid file name: ");
}
I have specified above where I want the program to return to within the code, I have currently tried creating a loop and then using continue however it wont let me put a try within the loop. Also I've tried to create a new void and it still wont work. Currently the program continues to run even if the user has entered an invalid file name.
I have searched for an answer already and can only find this relating to what I want: Java - Exception handling - How to re-enter invalid input
Also clarifying what I mean by putting a try in a loop; yes, it is possible. However I want to know whether for the continue to work in my program, do I put the try inside the loop or the loop inside the try? I have referred to: Should try...catch go inside or outside a loop?