[For Loop] Problem with logging in to my server.

I’ve been trying to get my logging in on my server to work for hours now!

I’ve been trying to find what was wrong with my code. I got registering an account working, and i figured that wasn’t the problem, so I tried to log into that account and even if I’m dead sure I’m right it says I failed.

After debugging this some more I narrowed it down to how I changed the log state in my account ArrayList controller thingy. Here’s the code:


public boolean login(String username, String password) {
		for(int i = 0; i < accounts.size(); i++) {
			TempAccount = accounts.get(i);
			
			if(TempAccount.getUsername() == username) {
				if(TempAccount.getPassword() == password) {
					TempAccount.setLogged(true);
					return true;
				}
			}
		}
		return false;
	}

The TempAccount is just well a Temporary variable and the accounts variable is an ArrayList with only Accounts in it. Each account has a username and a password here. And I’m looping through each account checking to see if any of the credentials match. I think the loop is broken but I don’t know. What did I do wrong? Do you need more source code because I got a good 3000 lines for ya. :stuck_out_tongue:

You have to use [icode].equals();[/icode] when comparing strings.

exactly.

the reason is strings are objects that hold a charsequence, they are not primitives

so comparing 2 string is like comparing 2 sheets of paper both saying the same thing, but they are not the same paper: 2 objects, coincidentally same characters

Indeed, both Longarmx and Cero are correct, Strings are annoying like that.

However they are also more interesting due to interning:

String s1 = "Hello";
String s2 = "Hello";
		
System.out.println(s1 == s2); // prints true
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
String s4 = new String();
s4 += "H" + "e" + "l" + "l" + "o";
		
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1 == s4); // false

s3 = s3.intern();
s4 = s4.intern();

System.out.println(s1 == s3); // true
System.out.println(s1 == s4); // true

OMG Thank you!

This little glitch was going to make or break my game right here!

Now I can continue onwards!

Thanks a million! No doubt I’ll be back with another stupid error soon. :stuck_out_tongue:

EDIT: IT WORKSSSSSSSS!!!

String.intern();

I did not know that. o_O