Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Safari File Edit View History Bookmarks Window Help 0 cse.usf.edu Harbinger HA60

ID: 3675283 • Letter: S

Question

Safari File Edit View History Bookmarks Window Help 0 cse.usf.edu Harbinger HA60 P Buying Guide: How Amazon.com: Audi BOSS - RC-300 Project 9: Countin www.cse.usf.edu/.. Incremental Java LASS UPEG rray.class DSC 0159.JPG 3,813 x 2,553 Project 9: Counting Integers UPEG File 000.jpeg enshot ...20.28.171,356 x 2,150 10 x 900 Write a program that reads an arbitrary number of integers in the range 1 to 50 from the keyboard and then outputs how many of each value were read. a PNG enshot ..20.28 0 x 900 y Draft for Project Do not output zero counts. ZIP Project 8.zip enshot ....28.21(2) 0 x 900 9 .Prompt the user for inputs as shown in the sample run. Let the user enter 0 to terminate input. Any other value outside the range 1 to 50 should get an error message. ZIP ensho StringArray.java .20.28.24 3.zip 979 bytes ZIP enshot ..20.28.26 0 x 900 2 MAR 6

Explanation / Answer

import java.util.Scanner;

public class CountArray {

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int num;
       // creating count array
       int []count = new int[51];
       System.out.print("First input: ");
       num = sc.nextInt();
       while(true){
           if(num == 0){// stop
               break;
           }
           if(num < 0 || num > 50){
               System.out.println(num+" is not a valid input.");
           }else{
               count[num]++; // incrementing count
           }
           //reading next input
           System.out.print("Next input: ");
           num = sc.nextInt();
       }
       sc.close();
       System.out.println();
       //printing count array
       System.out.println("Value       Count");
       for(int i=1; i<=50; i++){
           if(count[i] == 0)// ignore
               continue;
           System.out.println(i+":       "+count[i]);
       }
       System.out.println();
       System.out.println("Program completed.");
   }
}


/*

Output:

First input: 1
Next input: 2
Next input: 3
Next input: 11
Next input: 22
Next input: 1
Next input: 2
Next input: 45
Next input: 55
55 is not a valid input.
Next input: 66
66 is not a valid input.
Next input: 34
Next input: -1
-1 is not a valid input.
Next input: 0

Value       Count
1:       2
2:       2
3:       1
11:       1
22:       1
34:       1
45:       1

Program completed.

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote