Question
Code a Java console application that reads in seven integer values entered on a single line, and prints out the number of occurrences of each
Code a Java console application that reads in seven integer values entered on a single line, and prints out the number of occurrences of each value. Using Java single dimension arrays, the application counts the number of occurrences of each of the seven values. The application then the prints out the number of occurrences of each of the seven values to the screen.
*********************************************************************************************************************************************************************************************************************
As an example:
Enter 7 numbers: 2 88 2 64 9 201 5577
Number 2 was entered 2 times.
Number 88 was entered 1 time.
Number 64 was entered 1 time.
Number 9 was entered 1 time.
Number 201 was entered 1 time.
Number 5577 was entered 1 time.
*********************************************************************************************************************************************************************************************************************
import java.util.Scanner;
//public class
public class CountOccurence
{
//main class
public static void main(String args[])
{
int i, j, count = 0;
//array declration
int arr[]=new int[7];
//scanner object creation
Scanner in = new Scanner(System.in);
System.out.printf("Enter 7 numbers: ");
//loop for 7 times
for(i=0; i< 7; i++)
{
arr[i] = in.nextInt(); }
//nested loop to find the count
for(i=0; i<7; i++)
{
for(j=i; j<7; j++)
{
if(arr[i] == arr[j])
{
count++;
}
}
//display count for each digit
System.out.printf("Number %d was entered %d times. ",arr[i], count);
//set count to zero after each iteration
count = 0;
}
}
}
}
Only issue so far is that this code prints out all numbers entered. I need this program to print the numbersout but if it has duplicate numbers they only need to be printed out once.
Please look at the example output at the top of this question.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started