Domanda

I can't seem to figure out why I'm getting this error. I've tried putting everything in parenthesis and that helped the problem a little bit. It would be great if I could get some help.

Code :

public void merge(String[] result, String[] nameA, String[] nameB)
{
    int i1 = 0;   // index into nameA array
    int i2 = 0;   // index into nameB array

    for (int i = 0; i < result.length; i++) 
    {
        if (i2 >= nameB.length || (i1 < nameA.length && nameA[i1] <= nameB[i2])) 
        {
            result[i] = nameA[i1];    // take from nameA
            i1++;
        } 
        else 
        {
            result[i] = nameB[i2];   // take from nameB
            i2++;
        }
    }
}

error: bad operand types for binary operator '<='

È stato utile?

Soluzione 3

In addition to the answers of Luiggi Mendoza and Ivaylo Strandjev I like to point out that if you only want to make sure the strings differ, you can use equals like this:

if (i2 >= nameB.length || (i1 < nameA.length && !nameA[i1].equals(nameB[i2])))

Altri suggerimenti

In order to compare Strings in Java you need to call the method compareTo. Have a look at the Comparable interface that String implements.

<= and >= operators are for numeric primitive types like int or double. To compare Strings, use compareTo method.

nameA[i1].compareTo(nameB[i2]) < 0

If you want to compare Strings by length, then use <= operator on String#length instead:

nameA[i1].length() <= nameB[i2].length()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top