Java: I am comparing two strings but did not recognize it
I have this problem:
I wrote this function because I need to get the index of the occurrence of a particular string st
in a String array
static public int indicestring(String[] array, String st) {
int ret = -1;
for (int i = 0; i < array.length; i++){
if (st.equals(array[i])) {
ret=i;
break;
}
}
return ret;
}
Then I called:
System.out.println("indicestring(NODO,"ET2"));
and I got the correct number.
But when I do this:
String[] arcos2 = linea.split("-");//reading from a file and separating by "-"
String aux = arcos2[1];
System.out.println(arcos2[1]);
System.out.println(aux);
if (aux.equals(arcos2[1])) {
System.out.println("Is equal 1");
}
if (aux.equals("ET2")) {
System.out.println("Is equal 2");
}
if ("ET2".equals(aux)) {
System.out.println("is equal 3");
}
The first two prints were ET2
, but then it only printed from 3 ifs: "Is is equal to 1" .... The thing is, I have almost 200 nodes like "ET2" and only 3 don't work and give me - 1 in the first function ...
My question is: I am using the wrong arrays to store and compare data, because if aux = arcos2 [1] = "ET2" then why is' aux.equals ("ET2") 'or' arcos2 [1] .equals ("ET2 " ) does not work ? Is there another feature you can recommend to try? (I tried changing the equals with compareTo() == 0
and that didn't work either, and the fix was recommended.)
Earlier I had a similar error when I was comparing two arrays like this:
if(a[0] == b[0] && a[1] == b[1])
There was a case that was clearly right, but it was ignored ... But it got fixed when I changed it to:
if (Arrays.equals(a, b))
Maybe some changes like this
source to share