1

I'm trying to create another array but I want it to be the array double gainT[], which is the temperature in Fahrenheit, converted to Celcius to make another array called double gainTC[]. I am also trying to do the same thing but with the precipitation. I just can't seem to find a way to do it though.

 import java.io.IOException;
 import java.util.Scanner;

 public class AnnualClimate1 {
 public static void main(String [] args) throws IOException
{
    Scanner in = new Scanner(System.in);

    System.out.print("Choose the temperature Scale (F = Fahrenheit, C = Celsius): ");
    String tFC = in.nextLine();

    System.out.print("Choose the precipitation Scale (I = Inches, C = Centimeters): ");
    String pIC = in.nextLine();

    System.out.println("");
    System.out.println("");

    System.out.println("                 Climate Data");
    System.out.println("        Location: Gainesville, Florida");
    System.out.println("               Temperature " + tFC + "     Precipitation " + pIC);
    System.out.println("=================================================");

    double gainT[]={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};

    double gainTC[] = {(gainT[] - 32) / 1.8};

    double gainP[]={3.5, 3.4, 4.3, 2.9, 3.2, 6.8, 6.1, 6.6, 4.4, 2.5, 2.2, 2.6};

    double gainPC[] = {gainP[] / .3937};

    if(tFC.equalsIgnoreCase("F") && pIC.equalsIgnoreCase("I")){
        System.out.println("Jan.                " + gainT[1] + "               " + gainP[1]);
        System.out.println("Feb.                " + gainT[2] + "               " + gainP[2]);
        System.out.println("Mar.                " + gainT[3] + "               " + gainP[3]);
        System.out.println("Apr.                " + gainT[4] + "               " + gainP[4]);
        System.out.println("May                 " + gainT[5] + "               " + gainP[5]);
        System.out.println("Jun.                " + gainT[6] + "               " + gainP[6]);
        System.out.println("Jul.                " + gainT[7] + "               " + gainP[7]);
        System.out.println("Aug.                " + gainT[8] + "               " + gainP[8]);
        System.out.println("Sep.                " + gainT[9] + "               " + gainP[9]);
        System.out.println("Oct.                " + gainT[10] + "               " + gainP[10]);
        System.out.println("Nov.                " + gainT[11] + "               " + gainP[11]);
        System.out.println("Dec.                " + gainT[12] + "               " + gainP[12]);
    }
    else if(tFC.equalsIgnoreCase("C") && pIC.equalsIgnoreCase("C")){
        System.out.println("Jan.                " + gainTC[1] + "               " + gainPC[1]);
        System.out.println("Feb.                " + gainTC[2] + "               " + gainPC[2]);
        System.out.println("Mar.                " + gainTC[3] + "               " + gainPC[3]);
        System.out.println("Apr.                " + gainTC[4] + "               " + gainPC[4]);
        System.out.println("May                 " + gainTC[5] + "               " + gainPC[5]);
        System.out.println("Jun.                " + gainTC[6] + "               " + gainPC[6]);
        System.out.println("Jul.                " + gainTC[7] + "               " + gainPC[7]);
        System.out.println("Aug.                " + gainTC[8] + "               " + gainPC[8]);
        System.out.println("Sep.                " + gainTC[9] + "               " + gainPC[9]);
        System.out.println("Oct.                " + gainTC[10] + "               " + gainPC[10]);
        System.out.println("Nov.                " + gainTC[11] + "               " + gainPC[11]);
        System.out.println("Dec.                " + gainTC[12] + "               " + gainPC[12]);
    }

 }
 }
4
  • 3
    You need to loop through your gainT array, check each index, do the math, and store the result into an index of gainTC Commented Dec 2, 2015 at 19:10
  • How would I go about doing that? I still very new to programming and just learned loops last week. Could you show me a simple example? Commented Dec 2, 2015 at 19:13
  • sure, I posted a simple example. You can fill in the blanks :) Commented Dec 2, 2015 at 19:14
  • You need to do the calculation on each item in the list, something like: for(int i =0;i<gainT.length -1; i++) gainTC(i) = gainT(i)-32/1.8 Commented Dec 2, 2015 at 19:16

1 Answer 1

3

Here is an incomplete example so you can fill in the blanks yourself:

double gainT[]={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};

double gainTC[] = new double[gainT.length]; //create array which matches the size of gainT
//array.length is a property value returning the 'size' or length or the array

//Now just iterate through all the gainT[] values you populated above and read each one
for(int i = 0; i < gainT.length; i++){
     double math = //use your conversion math here, and store the value
     gainTC[i] = math; //assign the results of your math to the same spot in the new array
}

If you'd like more information on Loops and Arrays, check here:

For Loops: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

Arrays: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.