Results 1 to 15 of 15
  1. #1
    Hooky75 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default cant get .equals to work

    Can any one tell me why this won't work for me:

    Java Code:
    public static void mainMenu()
        {
            String userName, pass, string1;
            Scanner scnr = new Scanner(System.in);
                do{
                System.out.print("Please enter User Name:");
                userName = scnr.nextLine();
               }while(!checkLogin(userName));
                    System.out.println("b");
    }
    
    public static boolean checkLogin(String userName){
        for(int i = 0; i<users.size(); i++){
            if(userName.equals(users.get(i).getLogin())){
             return true;
            }
           }
          System.out.println("User not found");
          return false;
          }
    if use a syso and print the users.get(i).getLogin() i get:

    alan
    brad
    charlie
    dan
    eric

    but if user input is any of the above it still print "User not found".

    output so far:

    Please enter User Name:alan
    User not found
    Please enter User Name:brad
    User not found
    Please enter User Name:charlie
    User not found
    Please enter User Name:dan
    User not found
    Please enter User Name:eric
    User not found
    Please enter User Name:

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    db

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Use println to show what is being compared:
    System.out.println("testNm=" + users.get(i).getLogin() + "< vs userNm=" + userName + "<");

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    What is:
    Java Code:
    users.get(i).getLogin()
    returning?

  5. #5
    Hooky75 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    system.out.println(users.get(i).getLogin());

    returns

    alan
    brad
    charlie
    dan
    eric

    and userName returns whatever is inputed and they just won't compare even if they are the same string.

    And users.get(i).getLogin() is an Arraylist of login, password and id from a text file.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    No.
    I mean is it a String?

    ETA: Ah, I see it's an arrat list.

    So:
    Java Code:
    userName.equals(users.get(i).getLogin())
    Is comparing a String against an ArrayList...which is always going to be false since an ArrayList is not a String.

  7. #7
    Hooky75 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    ok is there a way for me to compare them.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Well, what are you trying to compare exactly?
    Once you have defined that, then you can write the comparison.

  9. #9
    Hooky75 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    still a newbie at this but am guessing i'm trying to compare an object reference to a string.

  10. #10
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Have you looked into casting? If your array list is storing strings, casting it to a string won't remove any information and you will be able to compare them.
    Last edited by Dark; 05-18-2011 at 11:56 AM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Quote Originally Posted by Hooky75 View Post
    still a newbie at this but am guessing i'm trying to compare an object reference to a string.
    That's not what I'm asking.
    What are you trying to achieve by this comparison.

    What do you want to compare?

    Don't think about the code...think about what you are checking for.

    Then you can go about trying to figure out how to do that check.

  12. #12
    Hooky75 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    i am checking to see if the name entered is in the arraylist.

  13. #13
    Hooky75 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    ok thank you all for the help i have found the problem the code i had does work but the arraylist was created from a text file with tokens and the login names were being created with 2 x Space and the end, have added trim() to eack token when reading and it now works.

    Once again your help has been greatly appreciated.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Dark View Post
    Have you looked into casting? If your array list is storing strings, casting it to a string won't remove any information and you will be able to compare them.
    Casting doesn't do anything; have a look:

    Java Code:
    public class T {
        public static void main(String[] args) throws Exception {
    
        	Object foo= "fred";
        	String bar= new String("fred");
        	
        	System.out.println(bar.equals(foo));
        	System.out.println(foo.equals(bar));
        }
    }
    'true' is printed twice, no matter the reference type of the objects. The equals() method of the real type (both String) is used. That's what inheritance and overriding is all about.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Hmm, well looks like I learned something from this topic. Thanks JosAH.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. c always equals a*b
    By imorio in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 02:32 PM
  2. == and equals()
    By arefeh in forum New To Java
    Replies: 13
    Last Post: 01-05-2010, 04:56 PM
  3. equals error
    By sysout in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-06-2009, 03:23 AM
  4. == is same as .equals()??
    By DrMath in forum New To Java
    Replies: 1
    Last Post: 09-30-2009, 04:57 AM
  5. Replies: 0
    Last Post: 01-25-2008, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •