In this program I'm working on is to readfile through my method named homophone which reads the file and uses a hashmap to find the words that has the most homophones and returns the count of the number of homophones that was the most. My problem is how do I read the file and pass it through the homophone method in the main method. Here is my code so far:
import java.security.KeyStore.Entry;
import java.util.*;
import java.io.*;
public class program2 {
public static void main(String[] args) throws IOException {
System.out.println(homophone());
}
public static int homophone(String file) throws FileNotFoundException{
HashMap<String,Integer> alike = new HashMap<>();
Scanner input = new Scanner(new File(file));
while(input.hasNext()) {
String line = input.nextLine();
String[] key = line.split(" ");
if(alike.containsKey(key[1])) {
alike.put(key[1],alike.get(key[1])+1);
} else alike.put(key[1],alike.get(key[1]));
}
int max = 0;
for(java.util.Map.Entry<String, Integer> e:alike.entrySet()) {
if(e.getValue()>max) max = e.getValue();
}
return max;
}
}
Example File Content:
A AH0
A(1) EY1
A'S EY1 Z
A. EY1
A.'S EY1 Z
A.S EY1 Z
A42128 EY1 F AO1 R T UW1 W AH1 N T UW1 EY1 T
AAA T R IH2 P AH0 L EY1
AABERG AA1 B ER0 G
AACHEN AA1 K AH0 N
AACHENER AA1 K AH0 N ER0
AAKER AA1 K ER0
AALSETH AA1 L S EH0 TH
AAMODT AA1 M AH0 T
AANCOR AA1 N K AO2 R
AARDEMA AA0 R D EH1 M AH0
AARDVARK AA1 R D V AA2 R K
AARON EH1 R AH0 N
AARON'S EH1 R AH0 N Z
AARONS EH1 R AH0 N Z
You need pass a String to your homophone method like that:
System.out.println(homophone("/folder/file.txt"));
Or
System.out.println(homophone(args[0]));
Using the second one, you will need to compile and use a system command line like that:
java program2 "/folder/file.txt"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.