double and Integer conversion
I am trying to figure this out:
double chiSquare = ((double)(hashtable.get(key).intValue()/noWords))/* * Math.log10((NO_DOCUMENTS/all.get(key)))*/;
if (开发者_Go百科key.equals("love")){
System.out.println(hashtable.get(key));
System.out.println(all.get(key));
System.out.println(noWords);
System.out.println(chiSquare);
System.out.println((double)1/841);
System.exit(0);
}
Why is it printing chiSquare, prints a zero, while printing 1/841 gives the double value?
The hashtable is of <String, Integer>
As @GregS indicated in his comment, an int divided by an int is an int. Casting one of the numbers to a double will produce the output your desire.
double chiSquare = ((double)hashtable.get(key).intValue())/noWords;
Or, use the convenience method on Integer to convert it to a double:
double chiSquare = hashtable.get(key).doubleValue()/noWords;
Because the parentheses are incorrect.
double chiSquare = ( (double) hashtable.get(key).intValue() )/noWords;
精彩评论