0

Hi, my codes if statement returns false for some reason. I tried different variation yesterday and today morning and nothing works. Could someone look my code. I got File file users.txt where the usernames are stored.

    public static void checkUser(String userName, char[] passWord) {
        FileInputStream fIn;
        DataInputStream dInput = null;
        try {
            fIn = new FileInputStream(file);
            dInput = new DataInputStream(fIn);
            BufferedReader lukija = new BufferedReader(new InputStreamReader(dInput));
            String userName2 = "";
            while (userName2 != null) {

                userName2 = lukija.readLine();
                System.out.println(userName2);
                System.out.println(userName);
                if (userName.equals(userName2)) {
                    System.out.println("sisään");
                }
            } 

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (dInput != null) {
            try {
                dInput.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

Actually username2 is stored in the users.txt and username comes from user input just a normal JTextField.

Edited by Viped

2
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by Viped
0

What output do you get from lines 12 and 13?
Also, print the length() of those two strings in case you have anything like trailing blanks or newline characters in there.

0

Both gives "Viped" (got user named Viped on the txt) but length is 7 and 5. UserName2 is 7 and UserName is 5. So bufferedreader read somehow it wrongly. There is no any newline character or blanks or anything else on a txt

0

Oh and adding System.out.println(userName2.length()); gives also nullpointerexception

Edited by Viped

0

You could try to trim() both strings to get rid of any leading or trailing white space.
The NPE must be because userName2 is null in that case.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.