Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/School-Work / School Work/WordFrequencies.java |
| Date | Not specified in source |
| Repository | Janady13/School-Work |
| Revision | 4a666c890caf7c43c47d0572dbcc287373c7a78e |
| Branch | main |
| Path or archive member | School Work/WordFrequencies.java |
| Original SHA-256 | fafed41f2682dc0e499b96d8f9578615f9f379f2b4afe64c25b14b7a4835370c |
| Git blob SHA | 25c1f1e51c11e02964e071fcaee3e8f7fe54de6d |
| Reading-copy SHA-256 | fafed41f2682dc0e499b96d8f9578615f9f379f2b4afe64c25b14b7a4835370c |
Recovered source transcript
Download complete source text ↓37 lines · 1.9 KB
import java.util.Scanner;
public class LabProgram {
// main method
public static void main(String[] args) {
// creating an object of scanner class named sc
Scanner sc = new Scanner(System.in);
// taking the number of words to be given input from the user and storing it in variable n
int n = sc.nextInt();
// creating an array of string of size n named arr
String arr[] = new String[n];
// loop to take the words input from the user
for (int i = 0; i < n; i++)
// taking the word input from the user and storing it at i th location in array arr
arr[i] = sc.next();
// for-each loop to count the frequency of each word
for (String s : arr)
// calling method getWordFrequency, passing array arr, number of elements in arr and the current word as argument and printing the value returned by the function
System.out.println(s + " " + getWordFrequency(arr, n, s));
// closing the object of scanner class
sc.close();
}
// defining method getWordFrequency that takes a string array, size of string array and a string as parameter
public static int getWordFrequency(String[] wordsList, int listSize, String currWord) {
// declaring and initializing an integer variable named count to 0
int count = 0;
// for loop to count to the frequency of the value in variable currWord in array wordsList
for (int i = 0; i < listSize; i++)
// checking if the value at i th location in array wordsList is equal to the value in variable currWord, ignoring the case
if (wordsList[i].equalsIgnoreCase(currWord))
// if they are equal then value in variable count is incremented by 1
count++;
// returning the value in variable count
return count;
}
}