-bash-4.2$ cat words.txt The wheels on the bus go rond and round. round and roun
ID: 3586465 • Letter: #
Question
-bash-4.2$ cat words.txt The wheels on the bus go rond and round. round and round round and round The wheels om the bus go rond and round, all through the town! The people on the bus go up and down. up and down. up and down The people on the bus go up and down, all through the town! The horn on the bus goes beep, beep, beep beep, beep beep beep, beep, beep. The horn on the bus goes beep, beep, beep all through the town! The wipers on the bus go swish, swish, swish. swish, swish, swish. swish, swish, swish. The wipers on the bus go swish, swish, swish, all through the town! The signals on the bus go blink, blink, blink blink, blink, blink blink, blink, blink The signals on the bus go blink, blink, blink all through the town! The motor on the bus goes zoom, zoomi, zoOm ZOO, ZOOT, L ZOO, ZOOT, L The motor on the bus goes zoom, zoom, zoom, all through the town! The babies on the bus go waa, waa, waa. Waa, waa, Waa. waa, waa, waa. The babies on the bus go waa, waa, waa, all through the town! -bash-4.2$ cat Preferences. java import java. util. Scanner import java. util. Map import java. util. Hashllap import java.io. File public class Preferences i public static void add(String fan, String team) i String value-preferences. get(team) if (value-null) preferences. put (team, fan) else preferences.put (team, value + * * fan) public static MapExplanation / Answer
Note : I just take few lines and typed in the words.txt file .My program is working fine.You many add more line to the input file..Thank YOu
__________________
words.txt
The wheels on the bus go round and round.
round and round.
round and round.
The wheels on the bus go round and round.
all through the town!
_________________
HistogramOfNoOfWordsInFile.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class HistogramOfNoOfWordsInFile {
//Creating a HashMap
static Map < String, Integer > m = new HashMap < String, Integer > ();
public static void main(String[] args) {
try {
String word;
//Opening an file
Scanner sc = new Scanner(new File("words.txt"));
/* This while loop continues to execute
* until it read each word until the end of the file
*/
while (sc.hasNext()) {
word = sc.next();
//calling the method
checkWordInMap(word);
}
Set < String > keys = m.keySet();
//Displaying the Histogram
for (String i: keys) {
System.out.print(i + ":");
for (int j = 1; j <= m.get(i); j++) {
System.out.print("* ");
}
System.out.println();
}
} catch (FileNotFoundException e) {
System.out.println("** File not Found **");
}
}
/* This method will check whether the word is available in the Hash Map
* or not if available increment its Corresponding value by 1
* if Not found add that word to the HashMap as the key
*/
private static void checkWordInMap(String word) {
//Checking whether the word is available in the HashMap
if (m.containsKey(word)) {
m.put(word, m.get(word) + 1);
} else {
//If not available Adding the word to the HashMap
m.put(word, 1);
}
}
}
__________________
Output:
The:* *
round.:* * * *
bus:* *
wheels:* *
town!:*
on:* *
round:* * * *
through:*
the:* * *
all:*
and:* * * *
go:* *
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.