Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Student) Employing a while loop to catch and reprompt for invalid user entries

Tags:

java

I am working to finish a program that uses a substitution cipher to encode and decode txt files. After quite a bit of work, my program is all but finished. The program functions by first asking the user if he/she wants to encode or decode a file. After answering this question, the program prompts the user to enter the name of the file that is being inputted (for en/decoding), and also the name of the output file that the program will send the new en/decoded text to. It looks a little something like this:

(double spaced for line breaks)

start of program

Do you wish to encode or decode: (user enters on console...let's use encode for example)

Enter name of input file: (user enters name of file here)

Enter name of output file: (user enters name of output file to be created by program)

end of program

Everything works fine. I have created various methods that do the encoding and decoding, but am having trouble with a part that should be quite easy considering the rest of this.

Among the methods that I wrote within the larger program, I have one that prompts a user to input the name of the output file (pasted below). However, I would like to write is so that if the user enters a name of a file that already exists, instead of overwriting it, I want to prompt the user and ask if he/she is sure they want to override the file by clicking "y" or "n" (will use the toLowerCase() method to ensure case value is okay.

I know that I want to use a while loop for this, but am having lots of trouble figuring out where (the try/catch portion seems to be giving me trouble). Can anyone offer me guidance on the best way to implement my while loop so that the user has indefinite number of options for inputting the name of the output file? Much thanks.

(I can always paste the entire code as well...just seemed as if not needed).

    public static PrintStream getOutputPrintStream(Scanner console){
    PrintStream output = null;
    while (output == null) {
        System.out.print("Enter output file: ");
        String fileName = console.next();
        try {
            output = new PrintStream(new File(fileName));
        }
        catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        }

    }
    return output;
}
like image 807
Rivers31334 Avatar asked Dec 04 '25 01:12

Rivers31334


1 Answers

To check the existance of the file you might use exists()-method. Therefore you need to create a File object before. Like

File f = new File(fileName);
if(!f.exists()){ // if the file does not exist
  // Your try-catch block here
}else{  // this means that there is a file with the given name
  System.out.println("You really want to override? y/n");
  String r = console.next();
  if(r.toLowerCase().equals("y")){
    // Your try-catch-block again
  }else{
    // Nothing here
    // to restart the loop.
  }
}

Best would be to implement it after

String fileName = console.next();

Anyways you could even extract your try-catch-block to a new function in your class.

like image 190
maximus_de Avatar answered Dec 05 '25 14:12

maximus_de



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!