Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: How to divide elements in two different arrays

1. ## How to divide elements in two different arrays

I am using an array to store 5 test scores. Then I have another array for the total possible score the test could have. I just need to find out what percent the grade is out of the total possible score.

```public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int[] grade = new int;
int[] outOf = new int;
int[] percents = new int;

int total = 0;
int average;
int percent;

grade = 70;
grade = 65;
grade = 120;
grade = 150;
grade = 30;

outOf = 210;
outOf = 100;
outOf = 150;
outOf = 200;
outOf = 50;

for(int i=0;i<grade.length;i++)
{
total += grade[i];

}

average = total / 5;

System.out.println("The total is " + total + "\nThe"
+ " average is " + average);
}

for(int j=0;j<grade.length;j++)
{

}
}```  Reply With Quote

3. ## Re: How to divide elements in two different arrays

I just need to find out what percent the grade is out of the total possible score.
Divide one by the other and multiply by 100.

Just a recommendation, but using an Object Oriented approach you could get the results fairly easily. For example, if you created a class called Test that contains the score and total possible score - the data is inherently packaged together in a way than can be easily understood and manipulated.  Reply With Quote

4. ## Re: How to divide elements in two different arrays

Yeah I know thats the formula on how to get the percent, but I dont know how to implement that. I have tried:
```         percents = (grade / outOf) * 100;

System.out.println(percents);```

I am getting 0 as an output though. Why is this?  Reply With Quote

5. ## Re: How to divide elements in two different arrays

grade = 70;
outOf = 210;
As you are using Integers:

70/210 will give you 0.

0 * 100 will give you 0.  Reply With Quote

6. ## Re: How to divide elements in two different arrays

You have to use floats or doubles instead. Integer doesn't provide you anything after decimal point.  Reply With Quote