Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Division issue 2

Status
Not open for further replies.

endoflux

Technical User
Aug 6, 2001
227
0
0
US
I've scratched my head too long: anyone?

The snippet below is part of a larger program that accepts user names and scores as user input, then calculates a percentage. I've only included the relevant pieces here.

My trouble is at (line 32):
Code:
percent[j] = (votes[j] / totalVotes);
As is, the program compiles and runs fine, but all values in percent[j] come back as 0.0. If I replace "totalVotes" with a literal '100', it works fine.

In troubleshooting, i've verified values exist in votes[] and totalValue. The issue seems to simply be that it won't divide anything by totalValue! Ideas?


Code:
import javax.swing.*;
public class Election {

	//data fields
	private int numCand;
	private int totalVotes;
	private String[] candidates;
	private int[] votes;
	private double[] percent;

	// ~~constructor method.
	public Election(int anumCand) {
		numCand = anumCand;
		candidates = new String[numCand];  //array for name of candidates
		votes = new int[numCand];  //array for votes
		percent = new double[numCand];  //array for percentage of votes
	}

	//~~This method will prompt user for a name and # of votes to fill each spot of the 'candidates' and 'votes' arrays
	public void setVoteCounts() {
		for(int i=0; i<numCand; i++) { //prompt user for names of candidates for # of candidates
			candidates[i] = JOptionPane.showInputDialog("Enter a Name for Candidate # " + (i+1));
			votes[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter number of votes for " + candidates[i]));
			totalVotes += votes[i];
		}
	}

	//~~This method will calculate the total votes entered, determine each candidate %, and store the value in the 'percent' array
	public void calcPercent() {
		for(int j=0; j<numCand; j++) { //calculate percentage of vote for each candidate
			percent[j] = votes[j] / totalVotes;
		}
	}
}
 
Integerarithmetic is performed, and as last step transformed to a double (too late).
I don't believe a literal 100 works.
Perhaps 100.0?
Code:
 public Rounding ()
	{
		double d;
		int a, b, c;
		a= 100;
		b= 55;
		c= 45;
		// bogus:
		d = b/a;
		System.out.println (d);
		d = c/100;
		System.out.println (d);
                // fine:
		d = ((double) b) /a;
		System.out.println (d);
		d = b / (double) a;
		System.out.println (d);
	}

don't visit my homepage:
 
This should also word, shouldn't it?

Code:
percent[j] = 1.0 * votes[j] / totalVotes;

Cheers,
Dian
 
Okay, we've got a winner!

By casting totalVotes (or whatever the divisor will be) as double, and/or adding '*1.0' in the divisor, it fixed the problem:
Code:
percent[j] = (votes[j] / (double) totalVotes);
Code:
percent[j] = (votes[j] / (totalVotes*1.0));

However, for others reading this, please note that the literal int '100' also worked fine, probably because a literal doesn't have a predefined type:
Code:
percent[j] = (votes[j] / 100);

Thanks guys!

 
However, for others reading this, please note that the literal int '100' also worked fine, probably because a literal doesn't have a predefined type:

Code:
 percent[j] = (votes[j] / 100);

No.
100 is an int.
100.0 isn't and 100f isn't and I can't reproduce your example.

Can you give small, runnable class?
Here is mine:
Code:
 public class Rounding
{
	public static void main (String args[])
	{
		int i = 55;
		double d = (i/100);
		System.out.println (d);
	}
}

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top