【发布时间】:2017-12-06 17:37:40
【问题描述】:
对于我的程序,我试图找出我的数据文件中长度为 3 个字母的单词的百分比。尽管每当我运行程序时,我都会收到一条错误消息,指出您不能除以 0。每次循环运行时,我都会将变量 wordCount 增加 1,但由于某种原因,我的程序将其识别为 0。任何人都可以帮助我我是如何收到这个错误的?
int threeLetters=0;
int wordCount=0;
while(inFile.hasNextLine()){
wordCount= wordCount +1;
String line = inFile.nextLine();
String[] word =line.split(" ");
int wordLength = word.length;
if (wordLength == 3){
threeLetters= threeLetters+1;
}
}
double percentage = wordCount/threeLetters;// error recieved here
这是程序正在读取的文本文件
Good morning life and all
Things glad and beautiful
My pockets nothing hold
But he that owns the gold
The sun is my great friend
His spending has no end
Hail to the morning sky
Which bright clouds measure high
Hail to you birds whose throats
Would number leaves by notes
Hail to you shady bowers
And you green fields of flowers
Hail to you women fair
That make a show so rare
In cloth as white as milk
Be it calico or silk
Good morning life and all
Things glad and beautiful
【问题讨论】:
-
看起来像 Integer division: How do you produce a double? 的副本,这个问题应该可以解决您的问题
-
分析异常信息。有哪些代码行等的确切信息。使用调试器。 SO 不是调试服务
-
@JacekCz 如果您阅读了这个问题,也不例外。问题是
smallerInt / biggerInt = 0.0 -
@phflack "我收到一条错误消息,指出您不能除以 0" 对我来说确实是个例外。
-
@lyah 但是,第一条评论中的链接是相关的,因为这将是您修复其他内容后的下一个问题。