Modulo separation of two integers

I keep getting the error "The% operator is undefined for the type of the argument (s) Integer, Integer" I'm not quite sure why this is happening. I thought that since modular division cannot return decimal numbers, that integer values ​​will be fine.

This happens inside the method in the program that I create. 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 not completed, but an error occurs when

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

and

  table[key % tableSize] = node; 

Any help or suggestions would be appreciated.

+6
source share
4 answers

I could get a sample Integer % Integer sample to compile in Java 1.5 and 1.6, but not 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 since Java introduced autoboxing and autounboxing in 1.5 , you should use the Java compiler from 1.5 to 1.5, say 1.4.

Solutions:

  • Transition to Java 1.5 / 1.6 / 1.7.
  • If you must use 1.4, use Integer.intValue() to retrieve int values ​​on which you can use the % operator.
+7
source

This works great 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 are using Java below 1.5.

+2
source

What you are trying here is called unboxing, automatically converting an object to a primitive type (another way is autoboxing).

Java docs have the following:


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

  • 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 are not doing one of these things, and although at first glance it seems that you are not passing your modal expression to the method and not assigning it to a variable, it really is, 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"; } } 

Another possibility is that you are using a pre-Java 5 environment where autoboxing and unpacking have been introduced.

The best bet in this case is probably to be explicit and use Integer.intValue() to get the base int .

However, you can also consider using int (not Integer ) for the key and only box it where you need it (when you add it to Entry ). It may be faster to use a primitive type, although you should, of course, test it to be sure.

+1
source

Try converting integers to int, and then run % .

 if(table[key.intValue() % tableSize.intValue()] == null) table[key.intValue() % tableSize.intValue()] = node; 
0
source

Source: https://habr.com/ru/post/943666/


All Articles