Array of Sum

I have been working of this program for quite a while now and can't seem to find out how to calculate the percentage of a number.

The output should read2 33.3333% of the sum but it comes out like this:2 3.0% of the sum.

Can you help me figure out where I went wrong.

This is the HW instructions: I am to write a program that reads in a list of int values, one perline and outputs their sum as well as all the numbers read in, with each number annotated to say what percentage it contributes to the sum. the program will ask the user how many integers there will be and I am to create an array of that length, and then fill the array with the integers input.

I got this far but can't seem to get my sum in percentage form.

import java.util.*;

publicclass ArrayOfSum

{

/** Reads a list of int values, one perline, and outputs their sum as well as all the numbers read in.

*/

publicstaticvoid main(String[] args)

{

int sum;

double percent;

Scanner keyboard =new Scanner(System.in);

System.out.println("How many numbers will you enter here:");

int index = keyboard.nextInt();

int[] value =newint [index];

System.out.println("Enter your numbers, one at a time:");

sum = 0;

for (index=0; index < value.length; index++)

{

value[index] = keyboard.nextInt();

sum = sum + value[index];//This is where all numbers are added up

}

System.out.println("The total sum is " + sum);

System.out.println("The numbers are: ");

for (index = 0; index < value.length; index++)

{

percent = 100 *(sum/value[index]);// this is where I am having my problem. I need to calculate percentage here.

//percent = Math.round(value[index]);// Creating a rounding of the total sum

System.out.println(value[index] +" " + percent+"% of the sum.");

}

System.out.println("Guess what? I am done with this weeks homework.");

}

}

[3047 byte] By [jackso3ta] at [2007-10-2 2:07:04]
# 1
An int divided by an int will result in an int. Try:percent = 100 *(sum/(double) value[index]);
CeciNEstPasUnProgrammeura at 2007-7-15 19:48:31 > top of Java-index,Java Essentials,Java Programming...
# 2
I tried putting double, but I didn't put it in parenthesis. I will try that and let you know.
jackso3ta at 2007-7-15 19:48:31 > top of Java-index,Java Essentials,Java Programming...
# 3
It's called a cast (or in this case: a primitive type conversion).
CeciNEstPasUnProgrammeura at 2007-7-15 19:48:31 > top of Java-index,Java Essentials,Java Programming...
# 4
You want the % of the sum? Then it should be:percent = 100 * ((double)value[index]/ sum);
PhHeina at 2007-7-15 19:48:31 > top of Java-index,Java Essentials,Java Programming...
# 5
Okay, so you don't need to have it rounded if I did it that way. I mean I don't have to use Mathround.(percent) right?
jackso3ta at 2007-7-15 19:48:31 > top of Java-index,Java Essentials,Java Programming...
# 6
The code you commented is rounding an integer anyway. :)If you want to round it, you have to round it. float to int is a simple truncation, if that's what you mean. 0.99 -> 0.
CeciNEstPasUnProgrammeura at 2007-7-15 19:48:31 > top of Java-index,Java Essentials,Java Programming...
# 7

you have to convert the ints to doubles before dividning, 100 times nothing is still nothing.

int 1 / int 2 will produce 0 if int 2 is bigger than int 1

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arithmetic.html

int i = 37;

int j = 42;

double x = 27.475;

double y = 7.22;

//dividing numbers

System.out.println("Dividing...");

System.out.println("i / j = " + (i / j));

//System.out.println("i / j = " + (Double) (i / j)); // won't compile inconvertible types

System.out.println("i / j = " + new Double(i / j)); // will be 0.0

System.out.println("i / j = " + new Double(i) / new Double(j)); // gives expected answer

System.out.println("x / y = " + (x / y));

harmmeijera at 2007-7-15 19:48:31 > top of Java-index,Java Essentials,Java Programming...
# 8
Thanks
jackso3ta at 2007-7-15 19:48:32 > top of Java-index,Java Essentials,Java Programming...
# 9
Since this is percent anyway you can do it like this:percent = new Double(100 *value[index]/sum).intValue();System.out.println(value[index] + " " + percent+ "% of the sum.");Where value[] is declared as int[] and sum is declared as int
harmmeijera at 2007-7-15 19:48:32 > top of Java-index,Java Essentials,Java Programming...