Reputation:
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.
Upvotes: 0
Views: 165
Reputation: 452
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.
Upvotes: 1
Reputation: 548
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;
}
Upvotes: 0