Search code examples
javajavafxbufferedreader

Unsure using BufferedReader to check info from .txt file against boolean


I am having a problem getting my code that reads from a .txt file to to check correctly. I can change the code to have it print the string out. But I cannot get it to check the string against the current info being entered. I get the error message I have coded, which pops up 3 times. My user.txt file is taking the username:password info in like this CPlank000:Tech8120 Here is the code,

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Button; 
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.io.*;
import javax.swing.JOptionPane;


public class Login extends Application {
private TextField tfUsername = new TextField();
private TextField tfPassword = new PasswordField();
private Button btLogin = new Button("Login");
private Button btClear = new Button("Clear");

@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create UI
GridPane gridPane = new GridPane();
gridPane.setHgap(5);
gridPane.setVgap(5);
gridPane.add(new Label("Username:"), 0, 0);
gridPane.add(tfUsername, 1, 0);
gridPane.add(new Label("Password:"), 0, 1);
gridPane.add(tfPassword, 1, 1);
gridPane.add(btLogin, 1, 3);
gridPane.add(btClear, 1, 3);

// Set properties for UI
gridPane.setAlignment(Pos.CENTER);
tfUsername.setAlignment(Pos.BOTTOM_RIGHT);
tfPassword.setAlignment(Pos.BOTTOM_RIGHT);

GridPane.setHalignment(btLogin, HPos.CENTER);
GridPane.setHalignment(btClear, HPos.RIGHT);

// Process events
btLogin.setOnAction(e -> checkLogin());

btClear.setOnAction(e -> {
    tfUsername.clear();
    tfPassword.clear();

});


// Create a scene and place it in the stage
Scene scene = new Scene(gridPane, 300, 150);
primaryStage.setTitle("Login"); // Set title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
 }

public void checkLogin() {
 try (BufferedReader br = new BufferedReader(new FileReader("user.txt")))
    {

        String sCurrentLine;

        while ((sCurrentLine = br.readLine()) != null) {
            if ("tfUsername" == sCurrentLine)  {
                if ("tfPassword" == sCurrentLine)
                JOptionPane.showMessageDialog(null, "Welcome!!");}
                else
                    JOptionPane.showMessageDialog(null, "Invalid Username or Password");    
            }


    } catch (IOException e) {
        System.out.println("Unable to find");
        System.out.println(e.getMessage());

    } 

}





  /**
   * The main method is only needed for the IDE with limited
  * JavaFX support. Not needed for running from the command line.
  */
  public static void main(String[] args) {
    launch(args);
 }
 }

Solution

  • problem:

               if ("tfUsername" == sCurrentLine)  {
                if ("tfPassword" == sCurrentLine)
    

    You are comparing the memory location of the strings not the true value of it thus it is always returning false, use the equals method instead.

    sample:

               if ("tfUsername".equals(sCurrentLine))  {
                if ("tfPassword".equals(sCurrentLine))
    

    Solution:

    You can split your string to get the username and password separately because what you are doing is that you are comparing the whole line of string

    while ((sCurrentLine = br.readLine()) != null) {
            String [] s = sCurrentLine.split(":"); //split the string
    
            if ("tfUsername".equals(s[0])) {
                if ("tfPassword".equals(s[1]))
                    JOptionPane.showMessageDialog(null, "Welcome!!");
            } else
                JOptionPane.showMessageDialog(null,"Invalid Username or Password");
        }