Results 1 to 4 of 4
  1. #1
    hellzxmaker is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default 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;
    	}

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Run a while loop around it that does not terminate, then when try{} successfully executes, break; the loop.

  3. #3
    hellzxmaker is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by hellzxmaker View Post
    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.
    It would repeat the check for the new file name if you get the file name within the while loop. Try it.

Similar Threads

  1. Replies: 4
    Last Post: 04-03-2010, 06:20 PM
  2. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 AM
  3. Filename Filter problem
    By KevMeistr in forum Advanced Java
    Replies: 2
    Last Post: 06-14-2008, 02:43 AM
  4. Replies: 5
    Last Post: 06-06-2008, 09:31 AM
  5. Reading a directory and getting the filename
    By mrjunsy in forum Advanced Java
    Replies: 1
    Last Post: 05-10-2008, 02:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •