0

I was trying to take an unknown number of integers from the user (until the user enters 0), and count the number of occurrences of each of the integers entered. I figured, to count after I'm done taking the integers, I'd have to store them in an array. And I did some research and realized that the only way to create an Array with an unspecified length, ArrayList is the only way. But my this part of the code is showing error:

import java.util.Scanner;

import java.util.ArrayList;

public class IntegersOccurence {

    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);

        System.out.print("Enter the integers between 1 and 100: ");
        ArrayList list = new ArrayList();
        System.out.println(list.get(0));
        //eclipse is showing an error in the line right below
        while (list.get(list.size() - 1) != 0) {
            list.add(input.nextInt());
        }   
    }
}
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • maybe you can refer to [this](https://stackoverflow.com/questions/18513308/what-is-the-difference-between-arraylist-arraylist-arraylistobject) as it can answer your question :) – seulgibear Nov 18 '17 at 06:44

3 Answers3

2

You are using raw type so type of the list is Object which cannot be compared to int (0) so use

 ArrayList<Integer> list = new ArrayList<>();

Read , What is a raw type and why shouldn't we use it?


As mentioned : you are adding no element in list and invoking get will cause a crash because

From docs

IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()

here index>=size() is true (list size is 0,no element) so hence exception

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0

Change below line:

ArrayList list = new ArrayList();

To:

ArrayList<Integer> list = new ArrayList<>();

By default the type of your list is Object, by defining the datatype we avoid runtime type errors and the check is being done in compile time.

HaroldSer
  • 2,025
  • 2
  • 12
  • 23
  • Thanks! That helped remove the error! But it's still showing an "Exception in thread "main"" public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print("Enter the integers between 1 and 100: ");
    ArrayList list = new ArrayList<>();
    boolean val = true;
    while (val == true ) {
    if (list.get(list.size() - 1) == 0) {
    val = false;
    }
    else {
    list.add(input.nextInt());
    }
    }
    }
    }
    – Fahim Siddiqui Nov 18 '17 at 07:02
  • what's the exception? – HaroldSer Nov 18 '17 at 07:03
  • Enter the integers between 1 and 100: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(Unknown Source) at java.util.ArrayList.get(Unknown Source) at IntegersOccurence.main(IntegersOccurence.java:16) – Fahim Siddiqui Nov 18 '17 at 07:05
  • @FahimSiddiqui exception is because there is no data in your list and `list.get(` you get index out of exception – Pavneet_Singh Nov 18 '17 at 07:05
  • This is what it's showing after I run the code – Fahim Siddiqui Nov 18 '17 at 07:05
  • Oh thanks a lot. Fixed it. – Fahim Siddiqui Nov 18 '17 at 07:06
  • Your "to" line uses a raw type. It should be `ArrayList list = new ArrayList<>();` – Andy Turner Nov 18 '17 at 08:01
0

You Should better use this code:

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

public class IntegersOccurence {

    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);

        System.out.print("Enter the integers between 1 and 100: ");
        List<Integer> list = new ArrayList<Integer>();
        list.add(input.nextInt());
        System.out.println(list.get(0));
        //eclipse is showing an error in the line right below
        while (list.get(list.size() - 1) != 0) {
            list.add(input.nextInt());
        }   
    }
}
Mehdi Ghasri
  • 488
  • 3
  • 10