-2
public String modUsernames[] = {"Test", "Test2"};
public String adminUsernames[] = {"Mod Jack", "Mod Demon"};

if (username.equals(modUsernames)) {
    rights = 1;
}
if (username.equals(adminUsernames)) {
    rights = 2;
}

How come this isn't working, and what would I do to get it working?

Gwenc37
  • 2,064
  • 7
  • 18
  • 22
  • you simply need to learn code concept of java . BTW you could more understand by this link http://stackoverflow.com/questions/767372/java-string-equals-versus – dharmendra May 27 '14 at 07:58

4 Answers4

3
(username.equals(modUsernames)) 

You should not compare String to Array and if username is an array than you can't check whether both are eqaul like this.

Use Arrays.equals(ar1,ar2) for this.

Or use array index and use loop to check one by one.

username.equals(modUsernames[0])

Or you can use Arrays.toString() and use contains method

Arrays.toString(modUsernames).contains(username);
akash
  • 22,664
  • 11
  • 59
  • 87
2

Assuming username is a String: The variable modUsernames is an array of Strings, it's not a single String. You have to compare the String username with one elemenet of that array:

if (username.equals(modUsernames[0]))

for example.

Note: The way you access to an element of an array is using an index:

someArray[index] 

Index in most programming languages start in 0. So, if the length of the array is 2 (i.e. it has two elements), you can only use the index 0 and 1.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
2

As stated by others you cannot compare a String to an String[], what you can do is to check whether the String is contained in the String[], e.g.:

if (Arrays.asList(modUsernames).contains(username)){
   rights = 1;
}
ebo
  • 2,717
  • 1
  • 27
  • 22
1

Here what is your intention to compare . If it is to compare arrays then you may go with Arrays.equal(arr1,arr2). or if your intention to compare the index vales then you can go string.equals(str2);

Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15