私のプログラムでは、データファイル内の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
これは、プログラムがwordCount
で
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
は[整数の除算の重複のようになります。どのようにダブルを生成するのですか?](https://stackoverflow.com/q/2909451/5475891)、その問題はあなたの問題を解決するはずです – phflack
例外メッセージを分析してください。コード行などの正確な情報があります。デバッガを使用します。 SOはデバッグサービスではありません –
@JacekCz質問を読んでも例外はありません。問題は 'smallerInt/biggerInt = 0.0'です。 – phflack