James S.
James S.

Reputation: 1

How do I cast an int to a double?

The purpose of this program is to intake 5 values (test scores) from the user, then output the average score. I am not familiar with arrays so I really don't have the slightest clue what I'm doing wrong. All I know is the double 'sum' cannot be set equivalent to the int ' total'. Sorry for being dumb but I'M TRYING HERE :)

import java.util.Scanner;

public class Main
{

  public static void main (String [] args)
  {

    int x = 0;
    double testScore[] = new double[5];
    double sum[] = new double[5];
    double total;
    int avg;

    Scanner keys = new Scanner(System.in);

    System.out.println("Enter the values of 5 separate test scores that you have received: \n");

    for (int i = 0; i < testScore.length; i++)
    {
        x++;
        System.out.println("Enter your grade for test number " +1);
        double score = keys.nextDouble();

        score = testScore[i];
        sum = testScore;
        sum = (int)total;
        avg = ((total) / 5);


        System.out.print("The sum of your grades is " +avg +"\n");
    }

  }
}

Upvotes: 0

Views: 182

Answers (4)

MC Emperor
MC Emperor

Reputation: 22967

In order to get the sum of all elements of an array, you will need to iterate over it:

Scanner keys = new Scanner(System.in);
double[] testScore = new double[5];
double sum = 0.0;

for (int i = 0; i < testScore.length; i++) {
    // We don't need to use x, we already have i
    System.out.println("Enter your grade for test number " + (i + 1));

    double score = keys.nextDouble();
    testScore[i] = score; // Store the grade into the element #i of the array

    // We can instantly process the data we have to collect. Otherwise we must
    // walk a second time over the elements
    sum = sum + score; // Or the shorthand notation: sum += score
}

double avg = sum / testScore.length;
System.out.println("The average of your grades is " + avg + "\n");

I modified the following things to your code:

  • You are using two arrays (testScore and sum) having the same function. Both are meant to store the grades in it. I removed one of them.
  • I removed x, because i fulfills its function already.
  • I changed the type of your variable avg to a double. Feel free to change it back to an int, but then the average's decimals will be truncated (for instance, 4.6 will be 4).

Upvotes: 0

Quinn
Quinn

Reputation: 11

first i would declare your variables like this:

int x = 0;
double[] testScore = new double[5];
double[] sum = new double[5];
double total;
int avg;

A few changes to be made:
You don't want to set score to testscore[i] because its null so flip that. If you want to cast the doubles to integers use Integer.valueOf(). You should also place them outside the for loop and calculate sum in the for loop, as shown:

for (int i = 0; i < testScore.length; i++)
{
    x++;
    System.out.println("Enter your grade for test number " +1);
    double score = keys.nextDouble();

    testScore[i] = score;
    sum += Integer.valueOf(score);
    total += score;

}
avg = Integer.valueOf(total / testScore.length);

System.out.print("The sum of your grades is " +avg +"\n");

I haven't tested this code but i hope it helps.

Upvotes: 0

BaSsGaz
BaSsGaz

Reputation: 784

Here you go, I tried not to change much of your code so you can still understand what I changed!

public static void main(String[] args) {
    double testScore[] = new double[5];
    double sum = 0;
    double avg;
    Scanner keys = new Scanner(System.in);

    System.out.println("Enter the values of 5 separate test scores that you have received: \n");

    for (int i = 0; i < testScore.length; i++) {
        System.out.println("Enter your grade for test number " + 1);
        double score = keys.nextDouble();

        sum += score;
        avg = sum / 5;

        System.out.print("The sum of your grades is " + avg + "\n");
    }
}

Basically, all you need is the sum variable, you can get the avg from it!

Upvotes: 0

Dmitry
Dmitry

Reputation: 85

double sum = 0;
for (double score: testScore) {
  sum += score;
}
double avg = sum / testScore.length;

Upvotes: 1

Related Questions