나는 두 개의 파일에 용어의 가중치가 포함되어 있습니다. 제 객체는 코사인 유사성을 계산합니다. cos = dotproduct (weight1, weights2)/euclidianDistance (weight1) * euclidianDistance (weight2)); 나는 무엇 Double.parseDouble
자바에서 2 arrylist의 코사인 유사성
Exception in thread "main" java.lang.NumberFormatException: For input string: "" 0.750305594399894" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538)
에 오류가
import java.io.*;
import java.util.*;
public class tp5
{
private static BufferedReader br1;
private static BufferedReader br2;
public static double getSimilarity(File file1, File file2)
throws IOException
{
br1 = new BufferedReader(new FileReader(file1));
String line1;
line1 = br1.readLine();
ArrayList<String> words1 = new ArrayList<String>();
for (String word : line1.split(" ")) {
words1.add(word);
}
br2 = new BufferedReader(new FileReader(file2));
String line2;
line2 = br2.readLine();
ArrayList<String> words2 = new ArrayList<String>();
for (String word : line2.split(" ")) {
words2.add(word);
}
int i;
int j;
int k;
// Double [] temp = null;
Double DotProduct = (double) 0 ;
Double euclid1 = (double) 0;
Double euclid2 = (double) 0;
for (j = 0; j < words1.size(); j++) {
DotProduct += Double.parseDouble(words1.get(j)) * Double.parseDouble(words2.get(j));
}
for (i = 0; i < words1.size(); i++) {
euclid1 = Math.pow(Double.parseDouble(words1.get(i)), Double.parseDouble(words1.get(i)));
}
euclid1 = Math.sqrt(euclid1);
for (k = 0; k < words1.size(); k++) {
euclid2 = Math.pow(Double.parseDouble(words2.get(k)), Double.parseDouble(words2.get(k)));
}
euclid2 = Math.sqrt(euclid2);
return DotProduct/(euclid1 * euclid2);
}
public static void main(String[] args)
throws IOException
{
File file1 = new File("texte.95-1.poids");
File file2 = new File("texte.95-2.poids");
System.out.println(getSimilarity(file1, file2));
}
}
내 무게는 예를 들어 체중이처럼 될 수있는 문제 = 0.750305594399894
:
이
내 코드입니다 해결책?
문제는 프로그램에서 ""(Exception이 알려주 듯이) 밖으로 두배를 만들고 싶어한다는 것입니다. 그래서 빈 줄이나 다른 것을 읽었을 것입니다 (어느 시점에서 Double.parseDouble (String)의 String은 비어 있습니다). –
@ ThomasBöhm 죄송합니다. 미안합니다. 내게 무엇을 제안합니까? 나를 모호합니다. –