0

I am working on a program in which I am reading a list of Celsius temperatures from a file, loading them into an array, and eventually loading another array with the corresponding Fahrenheit temperatures for each Celsius temperature.

When attempting to print the fahrArray, rather than displaying actual temperatures which were converted to Fahrenheit to Celsius, I am given this

[D@3d3b10b1>

Here is the code that I have so far.

import java.util.Scanner;
import java.io.*;

public class BonusLab {
  public static void main(String[] args) throws IOException {

    createdBy();

    double[] celArray = loadCelArray();

    printArray(celArray);

    double[] fahrArray = loadFahrArray(celArray);

  } // end main

  public static void createdBy() {
    System.out.println("Program created by-Beth Tanner");
  } // end createdBy

  public static double[] loadCelArray() throws IOException {
    Scanner fin = new Scanner(new File("weather.txt"));

    int x = fin.nextInt();

    double[] celArray= new double[x];

    for(int i = 0; i < celArray.length; i++)
      celArray[i] = fin.nextDouble();

    return celArray;
  } // end loadCelArray

  public static void printArray(double[] celArray) {
    for(int i = 0; i < celArray.length; i++)
    System.out.print(celArray[i] + " ");
  } // end printArray

  public static double[] loadFahrArray(double[] celArray) throws      IOException {
    Scanner fin = new Scanner(new File("weather.txt"));

double celsius = 

int x = fin.nextInt();

double[] fahrArray = new double[x];
for(int i = 0; i < fahrArray.length; i++) 
  fahrArray[i] = (9 / 5) * celsius + 32;

return fahrArray;
  } // end loadFahrArray

} // end class

Any help or suggestions would be greatly appreciated.

9
  • 1
    double celsius = I believe this is typo? Commented Dec 4, 2013 at 22:51
  • Please fix your formatting! Commented Dec 4, 2013 at 22:53
  • 3
    "cannot find symbol/variable (called i)" and "incomaptible types (cannot assign a double[] to a double)" - sounds clear enough Commented Dec 4, 2013 at 22:55
  • double celsius = celArray[i] is the right idea, but you did want to do this more than once, right, since you need to do this for every element of celArray? So move it inside the for loop. You will need to add curly braces. Commented Dec 4, 2013 at 23:00
  • 1
    I don't see the code that prints the fahrArray, but I'm just about positive that you are printing the array instead of the values in the array. Commented Dec 4, 2013 at 23:08

2 Answers 2

1

If I understood your code right, I think you should change this code

for(int i = 0; i < fahrArray.length; i++) 
    fahrArray[i] = (9 / 5) * celsius + 32;

to

for(int i = 0; i < fahrArray.length; i++) 
    fahrArray[i] = (9 / 5.0) * celArray[i] + 32;

You are trying to access celArray[i] at a position where i is not defined. In the for-loop i is defined and should be at the same position.

Another soluion is, that you can leave out the second scanning of the file, because u have already loaded the values. So it will be enough to loop through calArray.

Edit: user3008950 now provided the second possibility I wrote about.

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

3 Comments

Thank you, this corrected my original problem, I have updated the question with my current issue.
it will not give you correct answer. 9/5 will always result in 1, use 9/5.0 to get 1.8
@Smit thats right, I just wanted to correct the code. I'll update my answer.
0
public static double[] loadFahrArray(double[] celArray){
    double[] result=null;
    if(celArray!=null){
        result=new double[celArray.length];
        for(int i = 0; i < celArray.length; i++){ 
            result[i] = (9 / 5.0) * celArray[i] + 32;
        }
    }
    return result;
}

Comments

Your Answer

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