Java哈希表值求和
我有一个任务来计算hashtable<String, Double>
中的单词出现次数。我在网上找到了许多工作正常的代码,但问题是:
如何将所有这些事件相加,例如,如果我有三个词与它们的事件:
train = 2
java = 1
master = 4
我想得到这些事件的总和,比如这个总和=2+1+4=7
我用这行代码在控制台中获得的这些事件值
hashtable.get(key);
现在,当我创建一个变量来存储hashtable.get(key)
时,我得到的错误不能将double转换为int
当我使用
hashtable.get(key).intValue();
这是我的全部代码
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class main {
static final Integer ONE = new Integer(1);
public static String path;
public static String path1;
public static String path2;
public static String nazivRjecnika;
public static String key;
public static int Suma, Suma1, Suma2, Suma3 = 0;
public static int aj;
public static void main(String[] args) throws IOException {
String pathPolitika = "C:/Users/Lulu-Debela/Desktop/New folder/POLITIKA";
String pathShowbiz = "C:/Users/Lulu-Debela/Desktop/New folder/SHOWBIZ";
String pathSport = "C:/Users/Lulu-Debela/Desktop/New folder/SPORT";
String pathPRjecnik = "C:/Users/Lulu-Debela/Desktop/New folder/POLITIKA/rjecnik.txt";
String pathShRjecnik = "C:/Users/Lulu-Debela/Desktop/New folder/SHOWBIZ/rjecnik.txt";
String pathSRjecnik = "C:/Users/Lulu-Debela/Desktop/New folder/SPORT/rjecnik.txt";
String pathPolitikaRjecnik = "C:/Users/Lulu-Debela/Desktop/New folder/POLITIKA/rjecnik_politika.txt";
String pathShowbizRjecnik = "C:/Users/Lulu-Debela/Desktop/New folder/SHOWBIZ/rjecnik_showbiz.txt";
String pathSportRjecnik = "C:/Users/Lulu-Debela/Desktop/New folder/SPORT/rjecnik_sport.txt";
String line = null;
int ngramOffset = 3;
String files;
for (int g=0; g<3; g++){
if(g==0){
path = pathPolitika;
path1 = pathPRjecnik;
path2 = pathPolitikaRjecnik;
nazivRjecnika = "rjecnik_politika.txt";
}
else if(g==1){
path = pathShowbiz;
path1 = pathShRjecnik;
path2 = pathShowbizRjecnik;
nazivRjecnika = "rjecnik_showbiz.txt";
}
else if(g==2){
path = pathSport;
path1 = pathSRjecnik;
path2 = pathSportRjecnik;
nazivRjecnika = "rjecnik_sport.txt";
}
File folder = new File(path);
File oldrjecnik = new File (path1);
File oldrjecniktrigram = new File (path2);
File[] listOfFiles = folder.listFiles();
oldrjecnik.delete();
oldrjecniktrigram.delete();
for (int i = 0; i<listOfFiles.length; i++){
if (listOfFiles[i].isFile()){
files = listOfFiles[i].getName();
if (files.endsWith(".txt")|| files.endsWith(".TXT")){
if(!files.startsWith("rjecnik.txt")&&!files.startsWith(nazivRjecnika)){
File textFile = new File(folder.getAbsolutePath()+ File.separator + files);
try{
BufferedReader br = new BufferedReader(new FileReader(textFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(path1, true));
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
line = sCurrentLine;
for (String ngram : ngrams(ngramOffset, line))
writer.write(ngram + "\r\n");
}
br.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Hashtable<String, Double> hashtable = new Hashtable<String, Double>();
BufferedWriter writer = new BufferedWriter(new FileWriter(path2, true));
FileReader fr = new FileReader(path1);
BufferedReader br = new BufferedReader(fr);
String linee;
double p=0;
while ((linee = br.readLine()) != null) {
processLine(linee, hashtable);
}
Enumeration e = hashtable.keys();
while (e.hasMoreElements()) {
key = (String) e.nextElement();
writer.write(key + " : " + hashtable.get(key) + "\r\n");
hashtable.get(key);
}
p = hashtable.get(key).intValue(); //Here is the error I get
System.out.println(p);
writer.close();
br.close();
oldrjecnik.delete();
}
}
static void processLine(String line, Map map) {
addWord(map, line);
}
static void addWord(Map map, String word) {
Object obj = map.get(word);
if (obj == null) {
map.put(word, ONE);
} else {
int i = ((Integer) obj).intValue() + 1;
map.put(word, new Integer(i));
}
public static int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
} finally {
is.close();
}
}
public static List<String> ngrams(int n, String str) {
List<String> ngrams = new ArrayList<String>();
String[] words = str.split(" ");
for (int i = 0; i < words.length - n + 1; i++)
ngrams.add(concat(words, i, i+n));
return ngrams;
}
public static String concat(String[] words, int start, int end) {
StringBuilder sb = new StringBuilder();
for (int i = start; i < end; i++)
sb.append((i > start ? " " : "") + words[i]);
return sb.toString();
}
}
。。。这就是我得到的错误:
`Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
at main.main(main.java:125)`
# 1 楼答案
这个问题的根源是raw types的用法:
将
Hashtable<String, Double>
传递给此方法时,将丢失有关类型参数的信息。这就是为什么下面这行编译得很好:如果将方法签名更改为不使用以下原始类型:
您将能够在编译时发现这个问题
如果您查看^{} 的实现,您会注意到确实存在类型转换:
Double
和Integer
是不同的类(不要将它们与原语double
和int
混淆),它们中没有一个继承自另一个(尽管它们都扩展了Number
),因此您得到了ClassCastException
# 2 楼答案
下面是计算单词的代码
我不明白你为什么要用双倍,然后你会数数单词。如果你数单词,你将使用整数。那么你在铸造方面就没有问题了
# 3 楼答案
转这条线:
进入这一行:
我不知道你为什么要用双倍数词