5.17 LAB: Word frequencies Write a program that reads a list ofwords. Then, the program outputs those words and their frequencies.The input begins with an integer indicating the number of wordsthat follow. Assume that the list will always contain fewer than 20words.
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 arrayfor the frequencies.
below is the code im using please answer in Java
import java.util.Scanner;
public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int size; int count; int i; int j;
String[] words = new String[20]; int[] freq = new int[20]; size=scnr.nextInt(); for (i=0; i<size; i++){ words = scnr.next(); } for (i=0;i<size;i++){ count=0; for (j=0;j<size;j++){ if(words.equals(words[j])){ count++; } } freq = count; } for (i=0;i<size;i++){ System.out.println(words + " "+ freq); }}
}
* here is the error im getting*
Program errors displayed here
Exception in thread "main" java.util.InputMismatchException atjava.base/java.util.Scanner.throwFor(Scanner.java:939) atjava.base/java.util.Scanner.next(Scanner.java:1594) atjava.base/java.util.Scanner.nextInt(Scanner.java:2258) atjava.base/java.util.Scanner.nextInt(Scanner.java:2212) atLabProgram.main(LabProgram.java:15)
5.17 LAB: Word frequencies Write a program that reads a list of words. Then, the program outputs those words and their f
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am