user3394571
user3394571

Reputation: 21

Java code to count # of instances an item was entered in an array?

EDIT: I want to add that I am in a high school AP computer science class that is only at chapter6, I do not know what a heshmap is and I do not know what an array list is, I need to make this work by using For and While loops, and just arrays. I know I can do it only using that because my teacher did.

I'm working on a program that does exactly this for AP Compsci:

"Design and implement an application (IntegerCount.java) that reads a number of integers (in the range of 0 to 50 inclusive) that are entered by the user and counts how many times each integer is entered. After all input has been processed, print all of the values, with the number of times each one was entered. Hint: You will need a while loop, and make any number outside of the range the sentinel to end the program. Design: Output your results neatly, using tabs."

This so far right here is my code, which is the first half of the assignment.

package arrays; 
import java.util.Scanner; 
import java.util.Arrays; 
public class IntegerCount { 

    public static void main(String[] args) { 


            Scanner scan = new Scanner(System.in); 
            System.out.println("How many integers would you like to enter?"); 
            int aCount = scan.nextInt(); 
            int[] intcount; 
            intcount = new int[aCount]; 
            System.out.println("Enter "+ aCount +"# of Ints"); 
            for (int min = 0; min<intcount.length; min++){ 

                    Scanner scan1 = new Scanner(System.in); 

                    intcount[min]= scan1.nextInt(); 
                    aCount--; 
                    } 
            Arrays.sort(intcount); 
            System.out.print("| "); 
            for (int min = 0; min<intcount.length; min++){ 
                System.out.print( intcount[min] + " | "); 
            } 


    } 
}

However, I don't really understand or know how to implement the second part of the assignment which is to make it so they can't enter anything above 50, and that it counts the number of instances you enter a certain number. If anyone could add this code, and explain how it works that would be great, thank you. (I'm sure it will use a while loop, I haven't learned about an array list so don't implement it, thanks)

Upvotes: 2

Views: 3029

Answers (2)

bige
bige

Reputation: 1

not sure if this will help but my program is made to collect any number store it add it, then divide by the times the numbers were entered after typing in a specific number "999" to stop the while loop process.

hope this helps anyone, but you may have to modify it to your liking, cause im not sure how to explain exactly how to apply it directly.

import java.util.Scanner;

public class WhileLoop
{
    public static void main(String[] args)
    {
        @SuppressWarnings("resource")
        Scanner Input = new Scanner(System.in);

        double sum = 0;
        double num = 1;
        double counter = -1;
        double average = 0;

        while (num != 999)
        {
            counter++;
            average = (sum / counter);
            System.out.println("Enter a number, 999 to end: ");
            num = Input.nextInt();
            if (num != 999)
            {
                sum += num;
            }
            System.out.println("The sum of the numbers you entered: " + sum);
        }
        System.out.println(" your average is " + average);
    }
}

Upvotes: 0

Hugo Sousa
Hugo Sousa

Reputation: 1926

Just a little help without giving you much code.

You should ask a number until the input is outside the 0-50 range, so it would be something like this:

int userInput;

do{
   //ask input and set userInput variable
}while(userInput<=50 && userInput>=0);

EDIT: if you really want to use a while loop instead of a do-while loop, as you said in your edit (it's almost the same):

int userInput = 1; //just to ensure it enters the while at least once

while(userInput<=50 && userInput>=0){
   //ask input and set userInput variable
}

For the counting I can suggest you 2 solutions (the first one meets your edit requirements too):

  1. Create an array of ints (initialized with 0) with 51 positions. Everytime the user inputs a valid number, you increment the int in the position userInput - 1. This is probably the easiest way to implement in this simple example.
  2. Create an HashMap of <int, int>. The first int is the input, the second is the counter. Everytime the user inputs a valid number, you search for that number in the HashMap. If it already exists, increment the second. If it doesn't, create it and set the counter to 1.

Upvotes: 3

Related Questions