Question

Write a program that reads the integers between 1 and 100 and counts the occurences of each

Could someone please help me with the following;

Write a program that reads the integers between 1 and 100 and counts the occurences of each. Assume the input ends with 0. You should have two additional methodsbesides main. Their signature is:
public static void displayResults(int[] numList) - This method displays the results as shown below.

public static int getValidInput() - This method gets the input from the keyboard, makes sure it falls between 1 and 100 and if so returns the number to the callingfunction. If the number does not meet the criteria, then it should show an error message to the user and prompt for the number again.

Here is a sample run of the program:
Enter a number from 1 to 100. Enter 0 to end.
2
Enter a number from 1 to 100. Enter 0 to end.
5
Enter a number from 1 to 100. Enter 0 to end.
6
Enter a number from 1 to 100. Enter 0 to end.
5
Enter a number from 1 to 100. Enter 0 to end.
4
Enter a number from 1 to 100. Enter 0 to end.
3
Enter a number from 1 to 100. Enter 0 to end.
23
Enter a number from 1 to 100. Enter 0 to end.
43
Enter a number from 1 to 100. Enter 0 to end.
2
Enter a number from 1 to 100. Enter 0 to end.
0
A total of 9 numbers were entered.
The number 2 occurs 2 times
The number 3 occurs 1 time
The number 4 occurs 1 time
The number 5 occurs 2 times
The number 6 occurs 1 time
The number 23 occurs 1 time
The number 43 occurs 1 time
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class ReadIntegers
{
private static Scanner kb;

public static void main(String[]args)
{
kb = new Scanner(System.in);
private int[] list = new int[101];

int num;
int count = 0; // count #entered

// stop at 0
while((num = getValidInput()) >0)
{
// update count
count++;
// record number
list[num]++;
}

// print displayed numbers
System.out.println(+count+);
displayResults(list);
}

public static void displayResults(int[] numList)
{
for(int i = 1; i < numList.length; i++)
{
if(numList[i] > 1)
System.out.println(+i++numList[i]+);
else if(numList[i] == 1)
System.out.println(+i+);
}
}

public static int getValidInput()
{
while(true)
{
System.out.println();

int num = kb.nextInt();

if(num >= 0&& num <= 100)
return num;
else
System.out.println("Invalid.");
}
}
}

answered by: chelle
Add a comment
Know the answer?
Add Answer to:
Write a program that reads the integers between 1 and 100 and counts the occurences of each
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Count occurrence of numbers) Write a program that reads some integers between 1 and 100 and...

    (Count occurrence of numbers) Write a program that reads some integers between 1 and 100 and counts the occurrences of each. Here is a sample run of the program: Enter integers between 1 and 100: 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time 5 occurs 2 times 6 occurs 1 time 23 occurs 1 time 43 occurs 1 time 2 5 6 5 4 3 23 43 2 Note that if a number occurs more than...

  • Java Program 1. Write a class that reads in a group of test scores (positive integers...

    Java Program 1. Write a class that reads in a group of test scores (positive integers from 1 to 100) from the keyboard. The main method of the class calls a few other methods to calculate the average of all the scores as well as the highest and lowest score. The number of scores is undetermined but there will be no more than 50. A negative value is used to end the input loop. import java.util.Scanner; public class GradeStat {...

  • HW Help? I cannot seem to figure this one out.. --Write a program that reads the...

    HW Help? I cannot seem to figure this one out.. --Write a program that reads the integers between 1 and 100 and counts the occurrences of each. assume the input ends with 0. Example: 2 5 6 5 4 3 23 43 2 0 ENTER 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time 5 occurs 2 times 6 occurs 1 time 23 occurs 1 time 43 occurs 1 time Note that if a number occurs more...

  • Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds

    Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header:public static String convertMillis(long millis)For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, andconvertMillis(555550000) returns the string154:19:10.Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array). Output...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program...

    Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header: public static String convertMillis(long millis) For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, and convertMillis(555550000) returns the string 154:19:10. Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • C++counting occurrence of numbers

    write a program that read the integers between 1 and 100 and count the occurrence of each number. Assume the input end with 0. here is samole of the program:Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0 Enter2 occurs 2 times3 occurs 1 time4 occurs 1 time5 occurs 2 times6 occurs 1 time23 occurs 1 time43 occurs 1 time

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • Java Counts the occurrences of each digit in a string

    Write a method that counts the occurrences of each digit in a string using the following header: public static int[] count(String s) The method counts how many times a digit appears in the string. The return value is an array of ten elements, each of which holds the count for a digit. For example, after executing int[] counts = count("12203AB3"), counts[0] is 1, counts[1] is 1, counts[2] is 2, and counts[3] is 2. Write a test program that prompts the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT