**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。
上个月关门了。
改进这个问题
编辑2:在浏览了这些建议之后,我有两种可能的计算方法。1:
public int percent(String token) {
if(token != "" && token != "\n" && token != " " && map.get(token) != null) {
double ag = 0;
for (int x : map.values()) {
ag += x;
}
int retval = (int) (((double)map.get(token)) / ag); //mapsize only shows number of unique words; change to ALL words
return retval;
} else {
return 404;
}
} //*/
2:
public int percent(String token) {
if(token != "" && token != "\n" && token != " " && map.get(token) != null) {
double ag = 0;
for (int x : map.values()) {
ag += x;
}
int retval = (int) (((double)map.get(token)) / wordCount); //mapsize only shows number of unique words; change to ALL words
return retval;
} else {
return 404;
}
} //*/
最后的wordcount是一个class属性,每次构造函数添加一个值时,我都会增加一个。
老编辑:好吧,我修正了安迪在评论中指出的逻辑问题。我写的新方法是:
public int percent(String token) {
if(token != "" && token != "\n" && token != " " && map.get(token) != null) {
int ag = 0;
for (String x : map.keySet()) {
ag += map.get(x);
}
int retval = map.get(token)/ag;
return retval;
} else {
return 404;
}
} //*/
但是,我在driver中的测试仍然得到0的输出。我还擅自更改了test.txt及其下面的输出。
原职务:
我目前正在做一个项目,我必须从一个文件中获取输入,并使用hashmap来计算每个单词的频率。我们必须为“freqcount”类创建一些必需的方法,其中之一就是 public int percent(String token)
,它获取一个字符串,并尝试查找原始文件中有多少与之匹配。我假设最简单的方法是将已经计算出的字符串频率除以hashmap的大小。执行时,每次都返回零。
在这里和那里改变了事情并得到了结果之后,我发现 /
是什么导致值变为0。 +
, -
,和 *
好好工作。
从这里开始,我假设是整数类型将十进制舍入为零,但即使将函数更改为float或double,它仍然打印0.0。
司机:
package fint;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
public class Driver {
public static void main(String[] args) throws FileNotFoundException{
//---------------CONSTANTS---------------
final String PERC_STRING = "the"; //set these to whatever you want for testing purposes
final String COUNT_STRING = "the";
//---------------------------------------
File in = new File("test.txt");
Scanner scanFile = new Scanner(in);
ArrayList<String> parsed = new ArrayList<String>();
while(scanFile.hasNext()) {
parsed.add(scanFile.next().toLowerCase());
}
for(int i = parsed.size()-1; i>=0; i--) { //prints arraylist backwards
System.out.println(parsed.get(i));
} //*/
FreqCount fc = new FreqCount(parsed);
//----------------------------------------------------------------------------------------------
//percent() test
System.out.println("\nTest word '" + PERC_STRING +"' percentage: " + fc.percent(PERC_STRING));
//count() test
System.out.println("\nCount of '" + COUNT_STRING + "': " + fc.count(COUNT_STRING));
//printMap() test
System.out.println("\nHashmap: \n");
fc.printMap();
scanFile.close();
}
}
频率计数:
package fint;
import java.util.HashMap;
import java.util.List;
import java.lang.Math;
public class FreqCount {
//attributes----------------------------------------------------------------------------------------
private HashMap<String, Integer> map = new HashMap<String, Integer>();
//constructors--------------------------------------------------------------------------------------
FreqCount(List<String> driverList) {
for (int dLIndex = driverList.size() - 1; dLIndex >= 0; dLIndex--) {
String key = driverList.get(dLIndex);
if (map.get(key) == null) {
map.put(key, 1);
} else {
map.put(key, map.get(key) + 1);
}
}
}
/*FreqCount(List<String> driverList, int degree){ //will specify number (degree) of words in a token (" hello world " would one token of degree 2)
} //*/
//methods (required)---------------------------------------------------------------------------------
public int count(String token) { //returns value of a key
if(token != "" || token != "\n" || token != " ") {
return map.get(token);
} else {
return 0;
}
} //*/
//THIS METHOD v v v v v v v v
public int percent(String token) {
if(token != "" && token != "\n" && token != " " && map.get(token) != 0) {
int retval = map.get(token)/map.size(); //problem area
return retval;
} else {
return 404; //404 isn't special, just random code i chose for debug purposes
}
} //*/
//testing methods (not required)---------------------------------------------------------------------
public void printMap() {
for (String i : map.keySet()) { //loop ripped straight outta w3schools lol
System.out.println("key: " + i + " value: " + map.get(i));
}
} //*/
}
文本输入:
There were a number of factors that came together to contribute to the adaptive radiation of monkeys and decreased diversity of apes. The first involves the rifting of the African Plate somewhere between 60 and 10 million years ago. These plates split into what are known today as the Nubian plate and the Somali plate. This split happened alongside volcanic activity creating localized and varied environments. The different environments and food sources forced the populations of primates living in each to evolve through natural selection. The rift of the African plate combined with the phenomenon of continental drift caused the continents of Africa and Eurasia to be reconnected over time.
输出(百分比测试大约在中途):
Hashmap:
key: through value: 1
key: environments value: 1
key: forced value: 1
key: somewhere value: 1
key: years value: 1
key: these value: 1
key: that value: 1
key: number value: 1
key: split value: 2
key: time. value: 1
key: different value: 1
key: between value: 1
key: drift value: 1
key: 10 value: 1
key: africa value: 1
key: primates value: 1
key: plates value: 1
key: natural value: 1
key: in value: 1
key: involves value: 1
key: this value: 1
key: nubian value: 1
key: varied value: 1
key: continents value: 1
key: each value: 1
key: monkeys value: 1
key: as value: 1
key: environments. value: 1
key: million value: 1
key: were value: 1
key: creating value: 1
key: 60 value: 1
key: apes. value: 1
key: populations value: 1
key: continental value: 1
key: be value: 1
key: sources value: 1
key: activity value: 1
key: localized value: 1
key: contribute value: 1
key: plate value: 3
key: phenomenon value: 1
key: into value: 1
key: adaptive value: 1
key: diversity value: 1
key: known value: 1
key: are value: 1
key: and value: 6
key: of value: 8
key: today value: 1
key: came value: 1
key: together value: 1
key: over value: 1
key: somali value: 1
key: a value: 1
key: living value: 1
key: alongside value: 1
key: caused value: 1
key: plate. value: 1
key: decreased value: 1
key: ago. value: 1
key: food value: 1
key: happened value: 1
key: factors value: 1
key: the value: 12
key: rift value: 1
key: with value: 1
key: evolve value: 1
key: what value: 1
key: radiation value: 1
key: african value: 2
key: selection. value: 1
key: there value: 1
key: to value: 4
key: combined value: 1
key: rifting value: 1
key: eurasia value: 1
key: reconnected value: 1
key: volcanic value: 1
key: first value: 1
Test word 'the' percentage: 0
Count of 'the': 12
Adding 'indubidubly':
Count of 'indubidubly': 1
Adding 4 more...
Count of 'indubidubly': 5
感谢所有花时间读这篇文章的人!
1条答案
按热度按时间xggvc2p61#
所以你贴了很多代码,但实际上你的问题是在这一行和整数除法的使用。
因此,在处理您正在计算的内容之前,正如其他人所说的,该行应该如下所示。为了清晰起见,我引入了一些局部变量:
所以把这个比率计算成一个双精度(或浮点数),按100的比例换算成整数。
有几种方法可以达到同样的效果(比如将“frequeoftoken”和“totalwords”改为
double
删除公式中的类型转换。至于what-我将您的问题解释为“文件中所有标记(单词)中有多少百分比是一个特定的标记”。然而,您的实现没有太多意义,而是“文件中唯一单词的百分比是一个标记出现的次数。”
所以你需要像上面所做的那样改变这个等式,用总字数作为除数(我看你没有,所以我添加了一个新的方法,你必须写出来)。