0

I'm on with this problem for a week now..and i can't find what's really wrong with this. I'm comparing 2 strings. First is from a user input then the second is from the random access file read. the first string is a password converted to string and the 2nd is a password converted to string written to a random access file. here's my code snippet.

public boolean isRegistered(String pass, int id){
        boolean here = false;
        int i = 0;
        pass = padding(pass);
        String s;
        try{
            access = new RandomAccessFile(file, "r");
            do{
                access.seek(i*RECORD);
                access.getFilePointer();
                if(id == access.readInt()){
                    access.seek(i*RECORD+ID_SIZE);
                    access.getFilePointer();
                    s = String.valueOf(access.readUTF());
                    System.out.println("read s = "+s);
                    System.out.println("password is ="+pass);
                    System.out.println("read s.length = "+s.length());
                    System.out.println("pass.length = "+pass.length());
                    if(pass.equals(s){
                        here = true;
                        break;
                    }else{
                        System.out.println("Wrong password");
                        here = false;
                        break;
                    }
                }
                i++;
            }while(access.getFilePointer()!= access.length());
            access.close();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null,"isRegistered "+e.getMessage());
        } 
        return here;
    }

my file contains exactly the same password and thus it should return true. but unfortunately

updatedalready.. i really need your help. Thanks for taking time reading this :)

//i have known the problem :) Strings should have the same size and .equals should be imposed. thanks though :)

This code is working now

  • 2
    Don't compare strings with `==`. Use `equals()`. – Robby Cornelissen Aug 16 '14 at 07:59
  • Welcome to Stack Overflow! Please take a [tour]. – Unihedron Aug 16 '14 at 08:07
  • user password and the password written into file are exactly the same.. i even have padding spaces to the user password just to equal the size of the 2 strings. but it's not comparing right. it should be true. i even printed the strings and they print exactly the same strings.. with the same lengths.. i would really appreciate your help :) – Crievr Lumapac Aug 16 '14 at 08:10
  • i tried using .equals(s), same results still not working. – Crievr Lumapac Aug 16 '14 at 08:12
  • Please update your post with `equals` then (as otherwise everyone will focus on that), along with the output you're seeing. Was the data *written* with `writeUTF` by the way? – Jon Skeet Aug 16 '14 at 08:14
  • And why wasn't it hashed? – user207421 Aug 16 '14 at 08:49
  • yes. the data was written with writeUTF – Crievr Lumapac Aug 16 '14 at 10:40
  • So what's your question exactly? I suggest you post the writing code. I don't see any immediate need for all this RECORD stuff or seeking if you're only reading the file sequential. – user207421 Aug 16 '14 at 10:43
  • the question lately was "why does if(pass.equals(s)) not return true when in fact the record says the same value with the user input which is the password. :) thanks though – Crievr Lumapac Aug 16 '14 at 10:49
  • Because they weren't equal, even though they may have looked equal on the console. – user207421 Aug 17 '14 at 00:00

0 Answers0