primitive types can't convert to Wrapper for an ArrayList

66 Views Asked by At

I've been trying to learn how to use ArrayLists, however when I try to add primitive type values they don't convert to wrapper types in this machine.

import java.util.ArrayList;
import java.util.Scanner;

public class IntegerListTest {

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    ArrayList<Integer> integerList = new ArrayList<Integer>();
    ArrayList<Integer> integerListB = new ArrayList<Integer>();
    int n = 1;
    if (n > 0 && n < 100) {
      while ((n > 0) && (n < 100)) {
        System.out.println("Inserire numero");
        n = scan.nextInt();
        if (n > 0 && n < 100) {
          integerList.add(valueOf(n));
          if (integerListB.contains(n) == false)
            integerListB.add(n);
        }
      }
    }
    System.out.println(integerList);
    System.out.println(integerListB);
  }
}

For example in this code the int inputs can't convert to Integers. I tried on another computer and it works just fine and I don't know why.

edit.1: Thanks for all the replies, I really appreciate it. Many of you noted that the method

valueOf

in line 14 is incorrect and you're right. That was a correction I tried to solve the problem of this question. I then tried to run the code you guys suggested and it still gives me the same error:

"error: incompatible types: int cannot be converted to Integer integers.add( input );
^ Note: Some messages have been simplified; recompile with - Xdiags:verbose to get full output"

I believe my java auto-boxing isn't working but I'm not really sure.

2

There are 2 best solutions below

1
Basil Bourque On

tl;dr

You are working too hard. Simply add the primitive value to the object collection. Java automatically wraps (boxes).

 integers.add( input )

Details

I am guessing your problem starts with valueOf(n). You don't specify an object on which to call that method.

By the way, we can simplify the logic of your loop by using break.

The technical term you are looking for is boxing, wrapping a primitive value within an object.

Simply passing a primitive to the ArrayList#add method is enough to invoke auto-boxing. The compiler and/or runtime sees a int primitive value being added to a List of Integer objects, and automatically wraps that primitive in a necessary Integer object.

By the way, in Java 21+, SequencedCollection is the more general super-interface of List & ArrayList. See JEP 431: Sequenced Collections.

Scanner scanner = new Scanner( System.in );
SequencedCollection < Integer > integers = new ArrayList <>( );
int input = -1;
do
{
    System.out.println( "Specify integer from 1 to 99 inclusive: " );
    input = scanner.nextInt( );
    if ( input > 0 && input < 100 )
    {
        integers.add( input );  // Auto-boxing in play. The primitive `int` value in our var `input` is boxed into an `Integer` object.
    } else
    {
        break;
    }
}
while ( true );
System.out.println( "integers = " + integers );
Specify integer from 1 to 99 inclusive: 
7
Specify integer from 1 to 99 inclusive: 
42
Specify integer from 1 to 99 inclusive: 
-666
integers = [7, 42]
1
k314159 On

Your class compiles and runs just fine. For the error message "int cannot be converted to Integer" to appear, it must be picking up another class called "Integer". Make sure you don't have a class called "Integer" in the same directory, or anywhere in your CLASSPATH.

The built-in primitive wrappers, such as Integer, Character, etc., are defined in the java.lang package. This package is automatically imported by the Java compiler. So, when you refer to a class named Integer, it automatically picks up java.lang.Integer - unless, that is, you also have a class named Integer defined in the same place as the class you're currently compiling. Then your own definition of Integer takes precedence.

The moral is to avoid giving your classes the same name as an existing Java class.