Reputation: 7
My project is to create a program that does this:
The array of type double called grades has been initialized with the test scores for a class. The course instructor would like the highest score, the lowest score, and the mean value of all the grades.
I'm getting the following errors when I compile the following code:
File: C:\Users\Guest\Downloads\grades.java [line: 16]
Error: Type mismatch: cannot convert from double to int
File: C:\Users\Guest\Downloads\grades.java [line: 23]
Error: grades cannot be resolved to a variable
File: C:\Users\Guest\Downloads\grades.java [line: 23]
Error: Type mismatch: cannot convert from double to int
import java.util.Scanner;
public class grades
{
// global variable declaration
static Scanner cin = new Scanner(System.in);
static final double NUM_GRADES = 5;
public static void main(String[] args)
{
// declare variables
double highestGrade = -999999;
double lowestGrade = 999999;
double sumOfGrades = 0;
double avgGrades = 0;
double[] scores = new double[NUM_GRADES]; // array is initialized using a final variable
// use a for loop to obtain data from user using the final variable
for(double index=0; index < NUM_GRADES; ++index)
{
// this puts data into the current array index
grades[index] = cin.nextDouble();
// this calculates a running total of all the scores
sumOfGrades += grades[index];
// if current score in the array index is bigger than the current 'highestScore'
// value, then set 'highestScore' equal to the current value in the array
if(grades[index] > highestGrade)
{
highestGrade = grade[index];
}
// if current score in the array index is smaller than the current 'lowestScore'
// value, then set 'lowestScore' equal to the current value in the array
if(lowestGrade > grades[index])
{
lowestGrade = grades[index];
}
}
{
System.out.print("\nThe grade for student #"+(index+1)+" is: "+grades[index]);
}
// display the highest/lowest numbers to the screen
System.out.print("\n\nThese are the highest and lowest grades: ");
System.out.print("\n\tHighest: "+ highestGrade);
System.out.print("\n\tLowest: "+ lowestGrade);
// find the average
avgScores = sumOfGrades/NUM_GRADES;
System.out.print("\nThe average score is: "+ avgGrades);
// reset data back to 0 so we can find the ommitted average
sumOfGrades = 0;
avgGrades = 0;
}
}
Upvotes: 0
Views: 166
Reputation: 5033
To fix compile time errors.
import java.util.Scanner;
public class Grade //changed here
{
// global variable declaration
static Scanner cin = new Scanner(System.in);
static final int NUM_GRADES = 5;//changed here
public static void main(String[] args)
{
// declare variables
double highestGrade = -999999;
double lowestGrade = 999999;
double sumOfGrades = 0;
double avgGrades = 0;
double[] scores = new double[NUM_GRADES]; // array is initialized using a final variable
// use a for loop to obtain data from user using the final variable
for(int index=0; index < NUM_GRADES; ++index)//changed here
{
// this puts data into the current array index
//changed here
scores[index] = cin.nextDouble();
// this calculates a running total of all the scores
sumOfGrades += scores[index];//changed here
// if current score in the array index is bigger than the current 'highestScore'
// value, then set 'highestScore' equal to the current value in the array
if(scores[index] > highestGrade) //changed here
{
highestGrade = scores[index]; //changed here
}
// if current score in the array index is smaller than the current 'lowestScore'
// value, then set 'lowestScore' equal to the current value in the array
if(lowestGrade > scores[index])
{
lowestGrade = scores[index];//changed here
}
}
{
System.out.print("\nThe grade for student #"+(index+1)+" is: "+scores[index]);//changed here
}
// display the highest/lowest numbers to the screen
System.out.print("\n\nThese are the highest and lowest grades: ");
System.out.print("\n\tHighest: "+ highestGrade);
System.out.print("\n\tLowest: "+ lowestGrade);
// find the average
avgScores = sumOfGrades/NUM_GRADES;
System.out.print("\nThe average score is: "+ avgGrades);
// reset data back to 0 so we can find the ommitted average
sumOfGrades = 0;
avgGrades = 0;
}
}
Upvotes: 0
Reputation: 8598
Error: Type mismatch: cannot convert from double to int
- you are initializing an array using double as the size, which is not allowed, change NUM_GRADES type to int:
static final int NUM_GRADES = 5;
Error: grades cannot be resolved to a variable
- you don't have a variable called grades, that's why the compiler is complaining; probably your variable scores
should be called grades
Error: Type mismatch: cannot convert from double to int
- you are using double to index an array in for loop
, change it to int:
for (int index = 0; index < NUM_GRADES; ++index)
EDIT (few more):
variable grade
doesn't exist in this piece of code:
if(grades[index] > highestGrade)
{
highestGrade = grade[index]; //<------- change it to grades
}
your print() statement is outside for loop
, so index variable will not be accessible; move it inside the loop
avgScores
is also not declared as variable, it should probably be avgGrades, which you declared at the beginning
Upvotes: 3
Reputation: 4348
if(lowestGrade > grades[index])
...
System.out.print("\nThe grade for student #"+(index+1)+" is: "+grades[index]);
'index' would need to be an int (or cast to it) to be an array index.
This should fix you...
for(int index=0; index < NUM_GRADES; ++index)
Upvotes: 0
Reputation: 3660
NUM_GRADE is a double. You can't use doubles as an index to an array. Try declaring it as an int instead, like this:
static final int NUM_GRADES = 5;
You made the same mistake with index
. Declare it as an int.
grades
doesn't exist. You probably meant to use scores
.
Upvotes: 0
Reputation: 4298
For the "cannot convert from double to int" error, you can force a conversion by casting to int e.g. grades[index] = (int)cin.nextDouble();
but this amounts to "chopping off" the decimal places from the double and you end up with an int that is basically "rounded down" if that makes sense.
For the "grades cannot be resolved to a variable" error "grades" has not been declared as a variable whatsoever (like scores was).
Upvotes: 0
Reputation: 178343
You can only use an int
as the size in between brackets []
for an array size. Declare your NUM_GRADES
constant to be an int
, not a double
.
You declared an double array called scores
, but you don't use it, and then you refer to grades
which doesn't exist. Either change all grades
references to scores
or change the name of scores
to grades
.
Upvotes: 4