0

So I am trying to make a log in system which stores the usernames and password in a .txt file and when you log in it reads them and checks if the entered value and the value in the .txt file are matching. Unfortunately whenever i try to check is the input-ed value same as the value in the .txt file (is the entered username same as the username in the .txt file which I use as my database) i get a negative answer so here is the bugged part of the code:

public void actionPerformed(ActionEvent arg0) {

        LineNumberReader LineReader = null;
        try {
            LineReader = new LineNumberReader(new FileReader("C:/Documents and Settings/Josip/Desktop/PROGRAMING MOTHAFUKAZ/JAVA/Login/DataBaza.txt"));
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        String UserName = username.getText();
        System.out.println(UserName);



        try {
            String Line = LineReader.readLine();

            while(Line != null){

                UserName = username.getText();
                System.out.println(Line);
                Line = LineReader.readLine();

                if(Line == UserName){
                    System.out.println("LOGIN SUCCESFUL");
                }

            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 



    }

the username object is the JTextField

  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – RealSkeptic Aug 24 '15 at 10:39

4 Answers4

2

if (Line == UserName)
You are comparing reference of Line and UserName
To check contents use

if (Line.contentEquals(UserName))
Kashyap Kansara
  • 405
  • 4
  • 10
1

Check the following:

  1. Check the string comparison instaed of "==" use ".equalsIgnoreCase() or equals()"
  2. Readina text file is always problematic because even space is also considered as character while reading so use trim() before comparing the Username and Line to get that type of the errors.

Hope this helps!

0
Line == UserName

Use equals not == for string in java

Also you are setting userName value everytime you read a new line. I don't think you need it.

Jkike
  • 807
  • 6
  • 12
0

The Line == UserName is wrong. You should use " == " only when you compare numbers. For Strings you should use Line.equals(UserName) or even Line.equalsIgnoreCase(UserName).

Pol
  • 27
  • 7