
Patrick B. answered 12/10/20
Math and computer tutor/teacher
....and if the user does not input the correct # of words??????
This is a problem !!! smh
Also, getFrequencyOfWord() does not accept the ARRAY of frequency counts....
and btw you do not need to pass the # of words in the list because the array.length has that;
there is better way to do this, the design is very confusing to me :-x :-(
--------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class LabProgram
{
public static int getFrequencyOfWord(String[] wordsList, int listSize, String currWord)
{
int iFreqReturn = 0;
for (int iLoop=0; iLoop<listSize; iLoop++)
{
if (wordsList[iLoop].compareToIgnoreCase(currWord)==0)
{
iFreqReturn++;
}
}
return (iFreqReturn);
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
final int MAX_NUM_WORDS=20;
int N=-1;
while ((N<0) || (N>MAX_NUM_WORDS))
{
System.out.print(" Please input # of words [max=" + MAX_NUM_WORDS + "] :>");
N = scanner.nextInt();
}
String words[] = new String[N];
for (int iLoop=0; iLoop<N; iLoop++)
{
System.out.print("Please input word # " + (iLoop+1) + ":>");
words[iLoop] = scanner.next();
//System.out.println(words[iLoop]);
}
for (int iLoop=0; iLoop<N; iLoop++)
{
String curWord = words[iLoop];
int freqCount = getFrequencyOfWord(words,N,curWord);
System.out.println(" word = >" + curWord + "< : freq # = " + freqCount);
}
}
}