Results 1 to 15 of 15
Thread: cant get .equals to work
- 05-17-2011, 04:27 PM #1
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
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; }
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:
- 05-17-2011, 04:38 PM #2
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
db
- 05-17-2011, 05:10 PM #3
Use println to show what is being compared:
System.out.println("testNm=" + users.get(i).getLogin() + "< vs userNm=" + userName + "<");
- 05-17-2011, 05:21 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
What is:
Java Code:users.get(i).getLogin()
- 05-18-2011, 12:16 PM #5
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
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.
- 05-18-2011, 12:25 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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())
- 05-18-2011, 12:32 PM #7
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
ok is there a way for me to compare them.
- 05-18-2011, 12:35 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Well, what are you trying to compare exactly?
Once you have defined that, then you can write the comparison.
- 05-18-2011, 12:46 PM #9
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
still a newbie at this but am guessing i'm trying to compare an object reference to a string.
- 05-18-2011, 12:54 PM #10
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 12:56 PM.
- 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.
- 05-18-2011, 12:56 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
- 05-18-2011, 01:04 PM #12
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
i am checking to see if the name entered is in the arraylist.
- 05-18-2011, 01:26 PM #13
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
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.
- 05-18-2011, 01:28 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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)); } }
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 05-18-2011, 01:31 PM #15
Similar Threads
-
c always equals a*b
By imorio in forum New To JavaReplies: 3Last Post: 11-12-2010, 03:32 PM -
== and equals()
By arefeh in forum New To JavaReplies: 13Last Post: 01-05-2010, 05:56 PM -
equals error
By sysout in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-06-2009, 04:23 AM -
== is same as .equals()??
By DrMath in forum New To JavaReplies: 1Last Post: 09-30-2009, 05:57 AM -
name clash: equals(E) in and equals(java.lang.Object)
By AdRock in forum New To JavaReplies: 0Last Post: 01-26-2008, 12:13 AM
Bookmarks