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.

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.

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

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

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.

Okay the trim() works thanks for help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.