88.3k views
3 votes
Write a program that reads a list of words. Then, the program outputs those words and their frequencies. The input begins with an integer indicating the number of words that follow. Assume that the list will always contain fewer than 20 words.

Ex: If the input is:
5 hey hi Mark hi mark
the output is:
hey 1
hi 2
Mark 1
hi 2
mark 1
Hint: Use two arrays, one array for the strings and one array for the frequencies.

User Moin
by
5.0k points

1 Answer

5 votes

Answer:

Here is the JAVA program:

import java.util.Scanner; //to accept input from user

public class Main { //class name

public static void main(String[] args) { //start of main method

Scanner input = new Scanner(System.in); //creates Scanner object

int integer = input.nextInt(); //declares and reads the integer indicating the number of words

String stringArray[] = new String[integer]; //creates array to hold strings

for (int i = 0; i < integer; i++) { //iterates through the array

stringArray[i] = input.next(); } //reads strings

int frequency[] = new int[integer]; //creates array for frequencies

int count; //declares variable to count frequency of each word

for (int i = 0; i < frequency.length; i++) { //iterates through the frequency array

count = 0; //initializes count to 0

for (int j = 0; j < frequency.length; j++) { //iterates through array

if (stringArray[i].equals(stringArray[j])) { //if element at ith index of stringArray is equal to element at jth index of stringArray

count++; } } //adds 1 to the count

frequency[i] = count; } //adds count to ith index of frequency array

for (int i = 0; i < stringArray.length; i++) { //iterates through the array

System.out.println(stringArray[i] + " " + frequency[i]); } } } //displays each word in stringArray and its corresponding frequency in frequency array

Step-by-step explanation:

I will explain the program with an example:

let integer = 3

for (int i = 0; i < integer; i++) this loop is used to read input strings into stringArray.

At first iteration:

i = 0

0<3

stringArray[i] = input.next(); this reads a word and stores it to ith index of stringArray. Lets say user enters the string "hey" so hey is stored in stringArray[0]

i++ becomes i = 1

At second iteration:

i = 1

1<3

stringArray[i] = input.next(); this reads a word and stores it to ith index of stringArray. Lets say user enters the string "hi" so hi is stored in stringArray[1]

i++ becomes i = 2

At third iteration:

i = 2

2<3

stringArray[i] = input.next(); this reads a word and stores it to ith index of stringArray. Lets say user enters the string "hi" so hi is stored in stringArray[2]

i++ becomes i = 3

Now the loop breaks as i<integers evaluates to false.

Next the outer loop for (int i = 0; i < frequency.length; i++)

and inner loop for (int j = 0; j < frequency.length; j++) iterate through the array and if condition if (stringArray[i].equals(stringArray[j])) checks if any of the words at specified indices is equal. This is used in order to check if any word in the list comes more than once. So if any word occurs more than once in the array, then its count is increased each time and the count value is stored in frequency[] array to store the count/frequency of each word in the stringArray. Since hey occurs once so its count is set to 1 and hi occurs twice so its frequency is set to 2. So the output of the entire program is:

hey 1

hi 2

hi 2

The screenshot of the program along with its output with the given example in question is attached.

Write a program that reads a list of words. Then, the program outputs those words-example-1
User NoChecksum
by
4.6k points