0

I have been stuck on this code all weekend and I just need a pointer as to what I am missing. This is a school assignment so I do not want the answer I just want to know what I am doing wrong. I need to create a 2D Array that stores all user values of Fahrenheit that have been entered plus their conversions to celsius and kelvin. My Array will store the value but then clear it on the next iteration. Here is my relevant code and my most recent attempt:

   for (int i = 0; i <= 10; i++){
  //call method to show the main menu
  showMenu();

  System.out.print("Enter your selection: ");
     mySelection = keyboard.nextInt();

//switch statement to pick menu option selected. 
 switch(mySelection){

//calculate celcius
case 1: System.out.print("Please enter a Farenheit value: ");
enteredF = keyboard.nextInt();
showCelcius(enteredF);
popArray(enteredF, i);
break;

//calculate kelvin
case 2: System.out.print("Please enter a Farenheit value: ");
enteredF = keyboard.nextInt();
showKelvin(enteredF);
popArray(enteredF, i);
break;

It calls this portion that I am having an issue with:

public static float[][] popArray(float userValue, int i){
 float[][] dataHolder = new float[3][10];

  dataHolder[0][i] = userValue;
  dataHolder [1][i] = showCelcius(userValue);
  dataHolder [2][i] = (float)showKelvin(userValue);
  System.out.println("Farenheit: " + Arrays.toString(dataHolder[0]));
 System.out.println("Celcius: " + Arrays.toString(dataHolder[1]));
 System.out.println("Kelvin: " + Arrays.toString(dataHolder[2])); 

return dataHolder;

I need it to store all values in the array but it keeps clearing the previous entry. For example entering values of 0,25, and 10 give me:

For 0 entered:

Fahrenheit: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Celsius: [-17.777779, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Kelvin: [255.37222, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

For 25 entered:

Fahrenheit: [0.0, 25.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Celsius: [0.0, -3.8888888, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Kelvin: [0.0, 269.2611, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

For 10 entered:

Fahrenheit: [0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Celsius: [0.0, 0.0, -12.222222, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Kelvin: [0.0, 0.0, 260.92776, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

I would like my output to be:

 Fahrenheit: [0.0, 25.0, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 Celsius: [-17.777779, -3.8888888, -12.222222, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 Kelvin: [255.37222, 269.2611, 260.92776, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

Why does my array clear every time? I haven't been able to figure it out.

Daniella
  • 171
  • 2
  • 3
  • 14

0 Answers0