2

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)

user3394571
  • 21
  • 1
  • 3
  • 2
    I think the objective isn't asking how many number the user wants to input. You should ask a number until the user enters a number outside the 0-50 range. That's what I understood from the assignment, at least. And that's where you would need the `while` loop. – Hugo Sousa Mar 07 '14 at 22:20
  • If I "add the code", then do I get the class credit? However, we might be able to give you some hints to point you in the right direction. Since you're only supposed to count the numbers in the 0-50 range, you could start by setting up an array of 51 elements to hold counts. – ajb Mar 07 '14 at 22:25
  • 1
    Welcome to stackoverflow! please refer to the Ask Help section to aid you in formulating better questions: http://stackoverflow.com/questions/how-to-ask Please note, we are not here to do your homework :) – blurfus Mar 07 '14 at 22:25
  • To expound what Hugo Sousa wrote a little, my reading of the instructions is not that you need to make it impossible to enter a number outside the range 0-50, but that you should choose a number (-1 for instance) which the user is to enter in order to signify that they wish to terminate the program. – George Tomlinson Mar 07 '14 at 22:34
  • @George Tomlinson He doesn't actually needs to choose a number. If the user inputs a number lower than 0 or bigger than 50, then he should terminate the loop. – Hugo Sousa Mar 07 '14 at 22:37
  • I can't use an array list simply because I just haven't learned it yet. I really don't know what to do,. – user3394571 Mar 07 '14 at 22:42
  • Perhaps that is what is meant, but that's not how it reads to me, since 'the sentinel' is singular @Hugo Sousa. You may want to check this detail with the lecturer user3394571. You may also want to check that an ArrayList can't be used, since it would make things easier for the user, if not you too – George Tomlinson Mar 07 '14 at 22:45
  • Yeah, actually no need for ArrayList. See first suggestion from Hugo Sousa. – George Tomlinson Mar 07 '14 at 22:51
  • I updated the objective, re read the top. – user3394571 Mar 07 '14 at 22:58
  • @user3394571 See if my answer meets your objectives. – Hugo Sousa Mar 07 '14 at 23:12
  • Ok thank you, just one question, is userInput a separate variable or would I initialize it in my for loop when they first start inputing which i believe I tagged as intcount. – user3394571 Mar 08 '14 at 02:27
  • Your `intcount` is an array of ints. The `userInput` variable in my example is the `int` you read from the user input. It's similar to your `aCount`, where you do `scan.nextInt()`. – Hugo Sousa Mar 08 '14 at 18:42

2 Answers2

3

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.
Hugo Sousa
  • 1,904
  • 2
  • 15
  • 28
  • If they can enter an integer from 0 to 50 inclusive, the array needs 51 positions, not 50. – ajb Mar 08 '14 at 01:42
0

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);
    }
}
flob
  • 3,760
  • 2
  • 34
  • 57
bige
  • 1
  • It seems not to be a direct answer to this question, only somehow related. As the questioner seems to be only at basic level of Java you might want to add that required array for counting and enhance your question. – flob Jul 15 '14 at 15:00