Results 1 to 4 of 4
Thread: New filename after try catch
- 11-05-2010, 09:09 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
New filename after try catch
I cannot figure out how to ask the user for a new filename, after the computer throws a FileNotFound error at the end of my try catch block.
I need to ask the user for a string filename.
create a Scanner called input with that filename (must use try catch)
if the file doesnt exist, I need to reprompt the user for another filename.
my methods cannot change so everything must be done in the user interface() or the getInputScanner using the given parameters
Java Code:public class LabGrader { public static void main(String[] args) { //header(); userInterface(); } public static void userInterface() { Scanner console = new Scanner(System.in); System.out.print("Enter input filename or Q to (Q)uit: "); String filename = console.next(); if (filename.equalsIgnoreCase("q")) { System.exit(1); } while (!filename.endsWith(".lab")) { System.out.print("Input filename must end with .lab Please input new value or Q to (Q)uit: "); filename = console.next(); if (filename.equalsIgnoreCase("q")) { System.exit(1); } else if (filename.endsWith(".lab")) { filename = filename; } } Scanner input = getInputScanner(filename); } //Returns a Scanner for the input file named filename. //Returns null if the input file is not found. public static Scanner getInputScanner(String filename) { Scanner input = null; try { input = new Scanner(new File(filename)); } catch (FileNotFoundException e) { System.out.print(filename + " (No such file or directory)"); } return input; }
- 11-05-2010, 09:21 PM #2
Run a while loop around it that does not terminate, then when try{} successfully executes, break; the loop.
- 11-05-2010, 10:51 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
Wouldnt that just repeat the check for file existence though? If the check fails, I need to ask the filename again without exiting the method.
-
Similar Threads
-
get the size and filename of the jar file run from and SHA
By aztectrev in forum Advanced JavaReplies: 4Last Post: 04-03-2010, 06:20 PM -
how to catch two exceptions in one catch()?
By arnab321 in forum New To JavaReplies: 1Last Post: 11-06-2008, 10:54 AM -
Filename Filter problem
By KevMeistr in forum Advanced JavaReplies: 2Last Post: 06-14-2008, 02:43 AM -
how to display the picture when the filename is not in order
By norazanita in forum New To JavaReplies: 5Last Post: 06-06-2008, 09:31 AM -
Reading a directory and getting the filename
By mrjunsy in forum Advanced JavaReplies: 1Last Post: 05-10-2008, 02:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks