7.14 LAB: Word frequencies 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.

Respuesta :

Answer:

See explaination

Explanation:

Below is the program code;

import java.util.Scanner;

public class LabProgram {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int n = in.nextInt();

String[] words = new String[n];

for (int i = 0; i < words.length; i++) {

words[i] = in.next();

}

int count;

for (int i = 0; i < words.length; i++) {

count = 0;

for (int j = 0; j < words.length; j++) {

if (words[i].equals(words[j])) {

++count;

}

}

System.out.println(words[i] + " " + count);

}

}

}

Please kindly check attachment for the output.

Ver imagen kendrich

The program illustrates the use of arrays, loops and conditions.

The complete program in java, where comments are used to explain each line is as follows

import java.util.Scanner;

public class Main{

public static void main(String[] args) {

//This creates a scanner object

Scanner scnr = new Scanner(System.in);

//This declares the length of the array

int lent = scnr.nextInt();

//The declares the array for words

String[] wordList = new String[lent];

//The declares the array for the frequency of the words

int[] freq = new int[lent];

//The following iteration gets input for the words

for (int i = 0; i < lent; i++) {

   wordList[i] = scnr.next();

}

//This declares the count of each word

int count;

//This iterates through the array

for (int i = 0; i < lent; i++) {

   //This initializes the count of each word to 0

   count = 0;

//This iterates through the array again

   for (int j = 0; j < lent; j++) {

   //This following if statement check for the occurrence of the current word

       if (wordList[i].equals(wordList[j])) {

           count++;

}

}

//This populates the frequency array

freq[i] = count;

}

//The following iteration prints each word and its frequency

for (int i = 0; i < lent; i++) {

   System.out.println(wordList[i] + " " + freq[i]);

}

}

}

At the end of the program, the word and the corresponding frequency is printed

See attachment for sample run

Read more about arrays, loops and conditions at:

https://brainly.com/question/16766736

Ver imagen MrRoyal