-3

So I'm trying to count the number of integer occurrences in this program. The code still doesn't work, but am I on the right track?

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

        int userInput = 0;
        ArrayList<Integer> myList = new ArrayList<Integer>();
        int[] newArray = new int[myList.size()];
        int index1 = -1;
        int current;

        for (int num = 0; num <= userInput ; num++)
        {   
            System.out.println("Please enter a random number between 0 and 50, enter a negative number to end input: ");
            num--;

            if(userInput >= 0 || userInput <= 50)
            {       
                userInput++;
                userInput = scan.nextInt();
                index1++;
                myList.add(userInput);

            }
            if (userInput < 0 || userInput > 50)
            {
                myList.remove(index1);
                index1--;
                break;
            }
        }   

        for (int num2 = 0; num2 < myList.size(); num2++)
            {
                current = myList.get(num2);
                if(current == myList.get(num2))
                {
                    newArray[current-myList.get(num2)]++;
                }
            }

            for (int number=0; number < newArray.length; number++)
            {
                System.out.println(number + "1");
                System.out.println(" : " + newArray[number]);
            }
}
}

edit: just wanted to add that I can run the program, but when I input an integer that is out of bounds (not between 0 and 50), I get an error

studentS
  • 41
  • 1
  • 1
  • 5

2 Answers2

1

I just finished writing this, thanks for the downvotes while I was modifying my code and specifically stated I was modifying the code. Hashmaps will do the trick for you here.

public static void main(String args[]) {
        HashMap<Integer, Integer> numbers = new HashMap<Integer, Integer>();
        Scanner scan = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("Number: ");
            int current = scan.nextInt();
            if (current > 0 && current < 51) {
                if (numbers.containsKey(current)) {
                    numbers.put(current, numbers.get(current)+1);
                }else{
                    numbers.put(current, 1);
                }
            } else {
                if(numbers.isEmpty()){
                    System.out.println("Nothing inputted");
                    loop = false;
                }else{
                    for(Entry<Integer,Integer> e : numbers.entrySet()){
                        System.out.println(e.getKey() + " was entered " + e.getValue() + " times");
                    }
                    loop = false;
                }
            }
        }
    }
Andre
  • 778
  • 1
  • 5
  • 23
0

You do not need any additional collection like myList. You can declare array

int[] occurrences = new int[51];

which can store occurence of elements from range [0,50]. When You read n as nextInt from scanner You should increment element of this array at readed n-th position if n is in range or stop reading new values otherwise.

This if statemest is always true:

current = myList.get(num2);
if(current == myList.get(num2))

Code example:

public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int[] occurrrences = new int[51];

        while (true){
            int value = scanner.nextInt();
            if(value>=0 && value<occurrrences.length){
                occurrrences[value]++;
            }else{
                scanner.close();
                break;
            }
        }
        for (int i = 0; i < occurrrences.length; i++) {
            if(occurrrences[i]>0){
                System.out.println(i + " occured " + occurrrences[i] + " times.");
            }
        }
    }
gauee
  • 305
  • 3
  • 13