-1

The int should autobox but I do not know why it is not. When i try to compile, it gives me an incompatible types error. Is there something wrong with my code?

Scanner console = new Scanner(System.in);
    ArrayList<Integer> list = new ArrayList<>();
    int first = console.nextInt();
    while (first!=0) {
        first=console.nextInt();
        list.add(first);
    }
    System.out.println("Your first list: " + list);
}

2 Answers2

-1

You probably imported the library incorrectly. You should have something like that:

import java.util.ArrayList;
import java.util.Scanner;
MaCloudR
  • 11
  • 2
-2
  • try to used Java later than 5 or latest version of Java(Currently it is JDK11).
  • Make sure that your input is always integer.Otherwise you will face to "InputMismatchException".
    • Your code expected to get an integer, so if you give some string , your code will not work any more
  • make sure that , you have imported from right library.

    import java.util.ArrayList;
    import java.util.Scanner;
    
  • make sure your language setting level is 5.0 or higher. you should check the setting in your IDE setting.

Reza
  • 1,906
  • 1
  • 17
  • 21