hashtable<String, Double>
에 단어 수를 계산하도록 지정했습니다. 난 그냥 잘 작동 많은 코드 온라인을 발견하지만, 문제는 이것이다 :해시 테이블 값 합계
:
train = 2
java = 1
master = 4
나는 합계를 가지고 싶어 이 합계와 같은 그 발생 = 2 + 1 + 4 = 7
발생이 값을 내가 변수를 만들 때 내가
hashtable.get(key);
이제이 줄을 콘솔에서 얻을 그것을 저장하려면 hashtable.get(key)
오류가 발생하면 int로 double을 캐스팅 할 수 없습니다.
내가
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)`
'p' 선언문을 추가하고 문제의 실행 가능한 예제를 만들 수 있습니까? – Jens
정확한 스택 추적은 무엇입니까? 스택 트레이스가 가리키는 라인을 포함하여 게시하십시오. – Unihedron
그리고 그건 분명히 "전체 코드"가 아닙니다. 수입? 클래스 선언? 짧지 만 완전한 프로그램은 정말로 도움이 될 것입니다 ... 다음으로, 당신이 아마도 Hashtable를 원할 때'Hashtable '을 생성하고 있다는 것에 주목하십시오. (왜 Hashmap 대신 Hashtable을 사용합니까?) –