1

For my program I am trying to find out the percentage of words in my data file that are 3 letters long. Though whenever I run the program I receive an error stating that you can not divide by 0. I increase my variable wordCount by 1 every time my loop runs, yet for some reason my program recognizes it as 0. Can anybody assist me as to how I am recieving this error?

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

This is the text file the program is reading from

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
7
  • Looks like a duplicate of Integer division: How do you produce a double?, that question should solve your problem Commented Dec 6, 2017 at 17:39
  • analyze exception message. Has exact information which code line etc. Use debugger. SO is not debugging service Commented Dec 6, 2017 at 17:39
  • 1
    @JacekCz If you read the question, there is no exception. The issue is smallerInt / biggerInt = 0.0 Commented Dec 6, 2017 at 17:40
  • @phflack " I receive an error stating that you can not divide by 0" sure seems like an exception to me. Commented Dec 6, 2017 at 17:41
  • 1
    @lyah However, that link in the first comment IS relevant, because that will be your next problem after you fix the other stuff. Commented Dec 6, 2017 at 17:44

2 Answers 2

3

You are not processing your words correctly: you are counting three-word sentences, of which you have zero, rather than three-letter words. You need another for loop here:

while(inFile.hasNextLine()){
    String line = inFile.nextLine();
    for (String word : line.split(" ")) {
        wordCount++;
        int wordLength = word.length();
        if (wordLength == 3){
            threeLetters++;
        }
    }
}

In addition, you are not computing percentage correctly: threeLetters should be the numerator, not the denominator.

Finally, unless you'd like to truncate percentage to whole numbers, use double for your counters, or cast them before division:

double percentage = ((double)threeLetters)/wordCount;

Demo.

Sign up to request clarification or add additional context in comments.

Comments

2

You're not dividing by wordCount, you're dividing by threeLetters. And it is indeed 0 because nothing ever incremented it.

The problem with your logic is here:

String[] word =line.split(" ");
int wordLength = word.length;
if (wordLength == 3){
    threeLetters= threeLetters+1;
}

You're not counting how long the words are, you're counting how many words are on that line. And since no line in that file has exactly three words, the if is never true and threeLetters is never increased. So it remains 0.

What you need is another loop over that array. Something like this:

String[] words = line.split(" ");
for (int i = 0; i < words.length; i++) {
    int wordLength = words[i].length();
    if (wordLength == 3){
        threeLetters = threeLetters + 1;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.