62.7k 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 vectors, one vector for the strings and one vector for the frequencies.

User Anticro
by
3.2k points

1 Answer

5 votes

Answer:

The following are the program to the given question:

import java.util.*;//import package for user input

public class Make //define class Make

{

public static void main(String[] ask) //define a main method

{

Scanner obx = new Scanner(System.in); //declare the Scanner class object

int size=obx.nextInt(); //get the size of the array from the user

int a[]=new int[size]; //declare an integer array with given size

String word[]=new String[size]; //declare a string array with given size

for(int i=0;i<size;i++) //set the for loop

word[i]=obx.next(); //get string input from the user

for(int i=0;i<size;i++) //iterates with the array, increase the count

{

for(int j=0;j<size;j++) //defining a loop to check value

{

if(word[i].equals(word[j])) //use if to check that elements of words

a[i]++;//increamenting array size

}

}

System.out.print("\\"); //use print for break line

for(int i=0;i<size;i++)//set for loop to print the following result

System.out.println(word[i]+" "+a[i]);//print value

}

}

Output:

Please find the attachment file.

Step-by-step explanation:

  • First, establish the predefined package required and instead define the main class and define the main method within the class and method.
  • Declare the scanner class object and receive an object size in the 'size' variable thru the array.
  • Next create two arrays, the integer type "a" with both the input size given and the string type "word" with the input size supplied.
  • Define the loop for which string array elements are acquired and set two to again for loop, which increases with loop iteration the integer data type array by one.
  • Lastly, set the loop for which this result is printed.
Write a program that reads a list of words. Then, the program outputs those words-example-1
User Chris McKenzie
by
3.4k points