-1

I am pretty new to Java, kind of getting the hang of it, and am working on some code for a Caesar Cipher in my CS class. I have it able to read one line of code from a file and it can decrypt and encrypt just fine, but I think my teacher wants a full document to be able to be encrypted or decrypted. I know it's possible to have it read the entire document and store it into a string, but how would I go about doing that?

Thanks for any help! Let me know if you need to see my other Class, if you need it for reference.

    import java.util.Scanner;
    import java.io.*;

    public class Project3 {

    public static void main(String[] args) {
        Cipher caesar = new Cipher();
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter a filename to encode or decode.");
        String f = input.nextLine();
        String fileName = f;
        String line = null;
        System.out.println("Enter the number of steps to encode or decode the file by: ");
            int i = input.nextInt();
            input.nextLine();
        try {
            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            while((line = bufferedReader.readLine()) != null){
                System.out.println(line);
                System.out.println("What would you like to do? Enter [e] to encrypt or press [d] to decrypt.");
                String a = input.nextLine();
                    if(a.equals("e")){
                        String encoded = caesar.useCipher(line,i);
                        caesar.writeToFile(encoded);
                        break;
                        }
                    if(a.equals("d")){
                        String decoded = caesar.useCipher(line,(-i));
                        caesar.writeToFile(decoded);
                        break;
                        }
            }            
        }
        catch(FileNotFoundException ex){
            System.out.println("Unable to open file '" + fileName + "'");                
        }
        catch(IOException ex){
            System.out.println("Error reading file '" + fileName + "'");                  
          }
      }    
}
Rao
  • 20,781
  • 11
  • 57
  • 77
  • I strongly suggest you learn how to use your IDE's debugger so you can step through your code and examine variables at each line. That is a fundamental skill that you will need in order to become a good developer. That skill would have allowed you to solve this problem on your own. – Jim Garrison Feb 29 '16 at 05:24

1 Answers1

0

Your code already has the loop to process the entire file.

The only problem is the prompt asking what the user wants to do. THAT part belongs outside (before) the while loop.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190