How can I calculate count for an array of user inputted integers and loop the user input within a while statement?

281 views Asked by At

I have been trying to calculate count(occurrences) of each user inputted number in an array that is size 50. the user input cannot be outside the range of 0 to 50 or else it quits the program and prints results out. The results printed are the number and the count of each number each time user inputs said number. I seem to be having trouble comprehending For loops with this program, when the user inputs an outside value, it quits the program but does not print count or the value of the user inputted number(s). I feel like I am making a stupid simple mistake but thanks for any tips that could help me out with this.

'''

public class numberArray
{
   //------------------------------------------------------------------------- 
   // Declares array and integers, Then calculates occurences of each number      
   // and prints both value and number of occurences of each
   //------------------------------------------------------------------------- 
   public static void main(String[] args)
   {
      // Declaration and creation of array/integers
      int[] occurences = new int[51];
      int[] input = new int[51];
      Scanner scan = new Scanner(System.in);
      // Prompts user to enter input(s) in range
      System.out.print("Enter int value(s) inside 0-50 range (enter outside         
      range to exit): ");
      int num = scan.nextInt();
      // While Statement to calculate count for each user inputted number              
      //   UNTIL outside of range then prints
      // **** This is the part im having trouble with i believe, i cant get            
      // count to work and it seems to only print some random phrase of                
      // numbers/letters, unsure of what the correct calculation for count              
      // would be
      while (num >= 0 && num <= 50)
      {
         for (int count = 0; count < input.length; count++)
         occurences[num]++;
         for (int index = 0; index < input.length; index++)
         input[index]++;
         num = scan.nextInt();
      }
      System.out.print("Value: " + input + " Occurences: " + occurences);
   }
}

'''

1

There are 1 answers

0
Mr5he11 On

Every iteration of the main while loop iterates on a user input. So the logic should be:

  1. acquire the input as an int value
  2. increment the element of occurrences indexed by such value
  3. repeat

In other words you do not need the inner for loops. You only need to take the user's input number and to increment the related cell of your occurrences array. Be carefull to initialize all elements of occurrences to 0 though. When the user puts a value out of range the while loop terminates and you can simply iterate through the occurences array to print the user input (the index) and the count (the value). A code example follows:

public class numberArray
{
   //------------------------------------------------------------------------- 
   // Declares array and integers, Then calculates occurences of each number      
   // and prints both value and number of occurences of each
   //------------------------------------------------------------------------- 
   public static void main(String[] args)
   {
      // Declaration and creation of array/integers
      int[] occurences = new int[51];
      for (int i = 0; i < 51; i++) {
         occurences[i] = 0;
      }
      Scanner scan = new Scanner(System.in);
      // Prompts user to enter input(s) in range
      System.out.print("Enter int value(s) inside 0-50 range (enter outside         
      range to exit): ");
      int num = scan.nextInt();
      while (num >= 0 && num <= 50)
      {
         occurences[num]++;
         num = scan.nextInt();
      }
      for (int i = 0; i < 51; i++) {
         System.out.print("Value: " + i + " Occurences: " + occurences[i]);
      }
   }
}