'Issues with FileReader and Java GUI/jForm

I am trying to make a smaller version of Pwned Passwords (https://haveibeenpwned.com/Passwords) for my Ap comp sci project. Everything is goo besides 2 things:


(Issue 1) (image of my code to show better) I have this below my jForm source code which declares each button/etc and what they do. I get this error though: "Illegal static declaration in inner class PassCheck.check. I do not now how to resolve this issue.


The second issue is using FileReader and Buffered Reader. I want the program to read the text inputted from the jForm and compare it to a file which has a list of commonly used passwords. How can I do this? Here is my code so far of just practicing with FR and BR:

    import java.io.*;

public class MainFileReader {

  public static void main(String[] args) throws Exception{
    
      String refpass, input;
      input = "1234";
      
      FileReader fr = new FileReader("C:\\Users\\tcoley\\Downloads\\207pass.txt");
      BufferedReader br = new BufferedReader(fr);

    while((input = br.readLine()) != null){
        refpass = br.readLine();
        

And I stopped here. I apologize as Java is not my strong suit but any help is much appreciated!



Solution 1:[1]

For your issue #2 - input is the string variable that is to be used hold the password you want to find in the file yet you eliminate its contents when you apply it to reading a line: (input = br.readLine()). It will now hold the currently read file line (this is no good). You need to use the refPass variable instead, for example: (refPass = br.readLine()).

You only need to use br.readLine() once in your loop. What your code is effectively doing right now (if it runs) is reading two (2) file lines on each iteration of the while loop. It could potentially fall into an Exception since there is no protection for null in the second read. Again no good.

Once you've read a file line, ensure it actually contains something. A lot of times a file will have a blank line in it that can throw a monkey wrench into things if it's not handled. To check for this you can do something like what is shown below after a line is read into refPass:

while((refPass = br.readLine()) != null) {
    // remove leading & trailing whitespaces (if any).
    refPass = refPass.trim();
    // Skip past blank lines in file (if any).
    if (refPass.isEmpty()) {
        continue;
    }
    // .... rest of code ...
}

Now to complete your loop block code, you just need to compare the password read in with the password contained within the input variable (ex: "1234"). To do this, you could have something like this:

if (refPass.equals(input) {
    System.out.println("Password Found!")
    break; // Break out of the 'while' loop and close file.
}

On a side: Don't use == to compare Strings for content equality, that may not always work as you expect. Use the String#equals() method instead. Give the supplied link a read.

At the end of and outside your while loop, be sure to close the reader, for example: br.close(); so as to release hold on the file and free up resources.

Solution 2:[2]

You don't need to use BufferedReader. Buffering is only for inefficient reading and writing (ie doing multiple reads and writes)

Use Path and Files instead

Path p = "C:\\Users\\tcoley\\Downloads\\207pass.txt";
String file = new String(Files.loadAllBytes(p));

Solution 3:[3]

What does the file look like? There are a lot of ways to format a file and for simplicities sake, this will just assume it's one word per line:

With the line

    refpass = br.readLine();

You are taking in the line from the file

    boolean isEqual = refpas.equals(input);

This allows you to assess the line individually. Remember that '==' is not the way to use String comparisons in Java.

    ("cat" == "cat") != ("cat".equals("cat"))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DevilsHnd
Solution 2 ControlAltDel
Solution 3 Kemper Lee