On way to do it is to put the whole thing in a method. If it throws an exception, call the method again...
public void tryFile() {
System.out.print("Enter file name: ");
try { dataFile = scan.nextLine(); } // assume that scan is the class variable
catch (Exception e) {
System.out.println("The file was not found. Please input file name now: ");
tryFile();
}
}
Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
You would normally use a template something like this:
String fileName = "";
while (fileName.equals("")) {
try {
// get the file name - if successful fileName will not be ""
} catch(...) {
// do error message
fileName = ""; // force it round the loop again
}
}
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
try - catch - finally up to Java6, in Java7 try - finally
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224