142k views
4 votes
java 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 less than 20 words.

1 Answer

4 votes

Answer:

The following are the program in the Java Programming Language.

//set a package

import java.util.Scanner;

//define class

public class Main

{

//define a main method

public static void main(String[] args)

{

//declare the Scanner class object

Scanner s = new Scanner(System.in);

//get the size of the array from the user

int size=s.nextInt();

//declare an integer array with given size

int a[]=new int[size];

//declare a string array with given size

String word[]=new String[size];

//set the for loop

for(int i=0;i<size;i++)

//get string input from the user

word[i]=s.next();

//iterates with the array, increase the count

for(int i=0;i<size;i++)

{

for(int j=0;j<size;j++)

{

//check that elements of words

if(word[i].equals(word[j]))

//increament in the array by 1

a[i]++;

}

}

System.out.print("\\");

//set for loop to print the following result

for(int i=0;i<size;i++)

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

}

}

Step-by-step explanation:

The following are the description of the program.

  • Firstly, set the required predefined package and define class 'main then, define main method inside the class and inside the method.
  • Declare the object of the scanner class and get the size of the array through the object in the variable 'size'.
  • Then, declare two array which are integer type 'a' with the given input size and string type 'word' with the given input size.
  • Set the for loop that get string type array elements from the user and again set two for loop that increase the size of the integer data type array by 1 with the loop iteration.
  • Finally, set the for loop that print the following result.
User Tanekqua
by
4.3k points