We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,437 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Int isn't working correctly, any ideas why?

        int wordpercent = ((count/totalwords)*100);
                    JOptionPane.showMessageDialog(null, "Count: " + count);
                    JOptionPane.showMessageDialog(null, "Words: " + totalwords);
                    JOptionPane.showMessageDialog(null, "Character Count: " + length);
                    JOptionPane.showMessageDialog(null, previousletter);
                    JOptionPane.showMessageDialog(null, nextletter );
                    JOptionPane.showMessageDialog(null, "Your word makes up " + wordpercent + "% of the document");

This is the final part of my program code. The program lets you select a text file as well as a word and then shows various statistics like the total amount of words in the document, how many times your selected word shows up, etc. My problem is that I want it to show how much of a percentage of the document is made up from your selected word. I am using "int wordpercent = ((count/totalwords)*100); to show that. However, when I run the program everything works perfectly, except for this. It shows up as 0, which doesn't make sense to me since the count and totalwords variables are showing up correctly and are never 0.

3
Contributors
5
Replies
1 Hour
Discussion Span
8 Months Ago
Last Updated
6
Views
Question
Answered
ctclements
Newbie Poster
5 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I'm willing to be that count and totalwords are both int too, which means your division is performed in integer context. If the division results in a value less than zero, the precision is ignored and the result will be 0. You want something more like this:

int wordpercent = (int)((count / (double)totalwords) * 100)

This forces the division into floating point context, which retains precision so that it isn't lost before the multiplication step.

deceptikon
Challenge Accepted
Administrator
3,457 posts since Jan 2012
Reputation Points: 822
Solved Threads: 474
Skill Endorsements: 57

It's integer arithmetic, performed in integers throughout.
eg (10/20)x100
10/20 in integer = 0 - there's no fractions in integer values
0x100 = 0

on the other hand
(10x100)/20
10x100 = 1000
1000/20 = 50

JamesCherrill
... trying to help
Moderator
8,528 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

That was the problem, thank you guys. I'm new to java and programming in general. I changed "int totalwords" to "double totalwords". The only minor problem I have now is that because I changed totalwords to a double it displays "Total Words: 340.0". Is there any way to make it not display the .0?

ctclements
Newbie Poster
5 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Using a double is a valid solution, but it does have side effects. Personally I would just rearrange the expression so it does the multiply before the divide and keep all the variables as ints.
int wordpercent = (count*100)/totalwords;

JamesCherrill
... trying to help
Moderator
8,528 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

Thanks, that makes it look a little more clean.

ctclements
Newbie Poster
5 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 8 Months Ago by JamesCherrill and deceptikon

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0833 seconds using 2.78MB