Pregunta

I keep getting the error "The operator % is undefined for the argument type(s) Integer, Integer" I am not quite sure why this is happening. I thought that since modular division cannot return decimals that having integer values would be alright.

This is happening within a method in a program I am creating. The code is as follows:

    public void addToTable(Integer key, String value)
{
    Entry<Integer, String> node = new Entry<Integer, String>(key, value);
    if(table[key % tableSize] == null)
        table[key % tableSize] = node;
}

The method is unfinished but the error occurs at

    if(table[key % tableSize] == null)

and

    table[key % tableSize] = node;

any help or suggestions would be appreciated.

¿Fue útil?

Solución

I could get some sample Integer % Integer code to compile successfully in Java 1.5 and 1.6, but not in 1.4.

public static void main(String[] args)
{
   Integer x = 10;
   Integer y = 3;
   System.out.println(x % y);
}

This is the error in 1.4:

ModTest.java:7: operator % cannot be applied to java.lang.Integer,java.lang.Integer
       System.out.println(x % y);
                            ^

The most reasonable explanation is that because Java introduced autoboxing and autounboxing in 1.5, you must be using a Java compiler from before 1.5, say, 1.4.

Solutions:

  • Upgrade to Java 1.5/1.6/1.7.
  • If you must use 1.4, use Integer.intValue() to extract the int values, on which you can use the % operator.

Otros consejos

This works fine for me.

Integer x = Integer.valueOf(10);
Integer y = Integer.valueOf(3);

int z = x % y;

System.out.println(z);

No problems. Output:

1

What error are you getting? What version of Java are you using? It seems that you're using Java below 1.5.

What you're attempting here is called unboxing, the auto-conversion of an object to a primitive type (going the other way is autoboxing).

The Java docs have this to say:


The Java compiler applies unboxing when an object of a wrapper class is:

  • Passed as a parameter to a method that expects a value of the corresponding primitive type.
  • Assigned to a variable of the corresponding primitive type.

So one possibility is that you're not doing one of those things and, although it appears at first glance that you're neither passing your mod expression to a method nor assigning it to a variable, it's valid, at least in Java 6:

class Test {
    public static void main(String args[]) {
        Integer x = 17;
        Integer y = 5;
        System.out.println (x % y);
        String [] z = new String[10];
        z[x % y] = "hello";
    }
}

The other possibility is that you're using a pre Java 5 environment, where autoboxing and unboxing was introduced.

Best bet in that case is probably to be explicit and use Integer.intValue() to get at the underlying int.

However you may also want to consider using an int (not Integer) for the key and only boxing it up at the point where you need to (when you add it to an Entry). It may well be faster to use the primitive type, though you should of course benchmark it to be sure.

Try converting the Integers to ints, then run %.

if(table[key.intValue() % tableSize.intValue()] == null)
        table[key.intValue() % tableSize.intValue()] = node;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top