Buffered Read line on reads the first line

104 views Asked by At

I am writing a program that decrypts a text file that has been encrypted. My buffered reader only seems to read the first line of the text file only? This is shown by my decryption function() only printing out the first line only correctly decrypted. I have attached a snippet of my code but im not sure where it is going wrong. The b variable is to exhaustively decrypt the code, its irrelevant to the file reading

    BufferedReader br = new BufferedReader(new FileReader("decryptthis.txt"));
    String message;
    while (((message = br.readLine()) != null))
    {
        message = message.toUpperCase();
        while (b <= 26)
        {
            decryption(message, b);
            b++;
        }

This part is in my main function

public static String decryption(String message, int b) 

This is what my decryption function takes in

i think i need it to go in loop and continuously feed my decryption function the lines, however i thought this was done by the != null part

Any help would be appreciated it. Thank you

1

There are 1 answers

0
Melron On

First, try to put the correct code lines to the corresponding methods. Your problem is not located at the 'reading' part. How to confirm that? Just print what lines your buffer reads. After that, call your decryption method that inside includes all the decrypt codes.

For example,

final var path = Paths.get("PathToYourTxt");

try (var lines = Files.lines(path)) 
{
    lines.forEach(System.out::println);
} 
catch (IOException e) 
{
    e.printStackTrace();
}

this will print the content of the txt. (check it)

In your case,

final var path = Paths.get("PathToYourTxt");

try (var lines = Files.lines(path)) 
{
    lines.forEach(line -> decryption(line));
} 
catch (IOException e) 
{
    e.printStackTrace();
}
}

public static void decryption(String message) 
{
    // put your code here with all your checks with that B variable
}

Each method is doing something. Try not to stop your reading. Just abort the strings on your method with your conditions