|
-
May 15th, 2017, 06:38 PM
#1
Java Eclipse - Double Array Type Mismatch Errors
Hi, I'm a student writing a java temperature conversion program. The program includes the main driver, a class to read from a file "temps" which has an array of temparatures. The second class "Temp" is supposed handle the calculations with multiple methods. Because the array file has temperatures, there are ofcourse decimals. I'm getting red squiggly lines in the 2nd class where I reference the 1st method "convertToFAH" the message is "The method convertToFah(double[]) in the type Temp is not applicable for the arguments (double[], double, double)". I'm also getting a type mismatch error in the method convertToFAH, on the temps array. "Type mismatch cannot convert from double [] to double". Since the file includes an array don't I have to include the square brackets?
I'm almost at the end of the semester, but just haven't gotten the hang of converting different data types. How can I fix my program so the red squiggly lines go away?
Code:
public class Temp {
private String [] months = {"January", "February","March", "April", "May","June",
"July", "August","September","October","November","December"};
public Temp (double []temps, String months)
{
double avg = 0;
double max = 0;
String maxMonth = null;
double min = 0;
String minMonth = null;
double fahrenheit = 0;
double centigrade = 0;
convertToFah(temps, fahrenheit, centigrade); <<<<<<<<<<<<<<<<<<error 1 is here
}
public double convertToFah(double [] temps)
{ double fahrenheit = 0;
double centigrade = 0;
int a = Array.getLength(temps);
for(int i = 0; i < a; i++)
centigrade = temps; <<<<<<<<<<<<<<<<error 2 on the word temps
fahrenheit = centigrade * 9.0/5.0 + 32;
return fahrenheit;
}
Last edited by 2kaud; May 16th, 2017 at 04:08 AM.
Reason: Added code tags
-
May 16th, 2017, 08:09 AM
#2
Re: Java Eclipse - Double Array Type Mismatch Errors
1. The call of convertToFah must match the declared signature of convertToFah but it doesn't. The call is with three parameters but the signature has one only. This call will work because it matches the signature,
Code:
convertToFah(temps);
2. The centigrade variable is declared a double but you are trying to assign a whole array of doubles to it which won't work. To assign types they must either be of the exact same type or convertible/promotable according to the rules of the Java type system. This assigment will work because it assigns a double (one element (the first) of a double array) onto a double,
Code:
centigrade = temps[0];
Last edited by wolle; May 16th, 2017 at 08:23 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|