David
David

Reputation: 135

Temperature Conversion using Array not working?

so I'm trying to convert numbers in an array from fahreneheit to celcius; I'm not getting any errors, but my numbers are definitely not right. here is my code:

package lab7q2;

public class Lab7Q2 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here


    int[] tempList = {212, 95, 64, 32, 0, -12, -25};

    for (int i = 0; i < tempList.length; i++) {
        System.out.format("temp in F %4d  temp in C %6.2f \n", + tempList[i], tempFtoC(i));
    }


}

public static double tempFtoC (int tempList) {

    double tempC = ((tempList - 32)*5)/9;
    return tempC;
    }

}

and here is my output:

temp in F  212  temp in C -17.00 
temp in F   95  temp in C -17.00 
temp in F   64  temp in C -16.00 
temp in F   32  temp in C -16.00 
temp in F    0  temp in C -15.00 
temp in F  -12  temp in C -15.00 
temp in F  -25  temp in C -14.00 
BUILD SUCCESSFUL (total time: 0 seconds)

can anyone tell my why its not doing the conversion right? Is my method not set up correctly? (new to programming, so please don't be too rough with me...)

Upvotes: 0

Views: 1389

Answers (1)

badjr
badjr

Reputation: 2286

You are converting i, not tempList[i], so you should change your format statement to

System.out.format("temp in F %4d  temp in C %6.2f \n", + tempList[i], tempFtoC(tempList[i]));

Upvotes: 2

Related Questions