Write a java program that reads in and prints a text, line by line, and calls a
ID: 3719949 • Letter: W
Question
Write a java program that reads in and prints a text, line by line, and calls a series of methods as needed. The object of the program is to parse a sentence in order to figure out all the different words that are in the sentence and how many times each word occurs. Use two parallel arrays, one to store the different words (e.g., word[]) and one to store the appearance count for each word (e.g., wordCount[]). At the end, print the list of different words and their counts. (Extra Credit: print the list of words and their counts in alphabetical order.) You have to decide which methods to implement. For example, suppose the text is this: The elephant ate the banana and the giraffe ate the banana. The output would produces this list: and 1 ate 2 banana 2 elephant 1 giraffe 1 the 4 Hint: Think Bank Accounts! (Use what you learned in the Bank Accounts program.) Write a java program that reads in and prints a text, line by line, and calls a series of methods as needed. The object of the program is to parse a sentence in order to figure out all the different words that are in the sentence and how many times each word occurs. Use two parallel arrays, one to store the different words (e.g., word[]) and one to store the appearance count for each word (e.g., wordCount[]). At the end, print the list of different words and their counts. (Extra Credit: print the list of words and their counts in alphabetical order.) You have to decide which methods to implement. For example, suppose the text is this: The elephant ate the banana and the giraffe ate the banana. The output would produces this list: and 1 ate 2 banana 2 elephant 1 giraffe 1 the 4 Hint: Think Bank Accounts! (Use what you learned in the Bank Accounts program.) Write a java program that reads in and prints a text, line by line, and calls a series of methods as needed. The object of the program is to parse a sentence in order to figure out all the different words that are in the sentence and how many times each word occurs. Use two parallel arrays, one to store the different words (e.g., word[]) and one to store the appearance count for each word (e.g., wordCount[]). At the end, print the list of different words and their counts. (Extra Credit: print the list of words and their counts in alphabetical order.) You have to decide which methods to implement. For example, suppose the text is this: The elephant ate the banana and the giraffe ate the banana. The output would produces this list: and 1 ate 2 banana 2 elephant 1 giraffe 1 the 4 Hint: Think Bank Accounts! (Use what you learned in the Bank Accounts program.)Explanation / Answer
Below I had written code for finding the frequency of words and printing it.
Hope this helps...
Thankyou..... :)
import java.util.*;
class count{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter string:");
String str = in.nextLine();
String str1 = str.replace(".","");
//spliting sentence with spaces
String[] words = str1.split("\s+");
String[][] words_count = new String[words.length][2];
for(int i=0;i<words.length;i++){
//storing words in words_count array
words_count[i][0]=words[i];
int c=0;
//counting the frequency of words
for(int k=0;k<words.length;k++){
if(words[i].equalsIgnoreCase(words[k]))
c++;
}
words_count[i][1]=String.valueOf(c);
}
System.out.println("The output produces ths list:");
for(int i=0;i<words_count.length;i++){
System.out.print(words_count[i][0]+":"+words_count[i][1]+" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.