Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Array help

Options
  • 17-03-2016 2:01pm
    #1
    Closed Accounts Posts: 191 ✭✭


    Hi can anyone please explain to me why my Array does not display the 12 numbers imputed by the user. I'll post my code so far. Thank you!


    double temp;
    double total = 0;

    double TemperatureArray [] = new double [12];

    for (int i=0; i<12; i++){
    String tempStr=JOptionPane.showInputDialog("Enter the temperature of each month");
    temp=Double.parseDouble(tempStr);


    }
    for (int j=0; j<TemperatureArray.length; j++){
    TemperatureArray[j]=total;

    }

    JOptionPane.showMessageDialog(null, "The temperatures are " + total , "Results",
    JOptionPane.INFORMATION_MESSAGE);
    }

    }


Comments

  • Registered Users Posts: 773 ✭✭✭pillphil


    You might have got it already, here's an explanation anyway.

    double temp;
    double total = 0;

    double TemperatureArray [] = new double [12];


    // Assume input is [1, 2, 3…11, 12]
    for (int i=0; i<12; i++){

    // take input from user
    String tempStr=JOptionPane.showInputDialog("Enter the temperature of each month");
    // overwrite value in temp each time
    temp=Double.parseDouble(tempStr);

    // After first pass, temp == 1…After 12th pass, temp == 12
    }
    // temp now == 12
    // total now 0
    for (int j=0; j<TemperatureArray.length; j++){
    TemperatureArray[j]=total; // assign value of total to temperature array[j]
    // first pass - temperature [0] is assigned value 0
    // second pass - temperature [1] is assigned value 0
    //…
    // twelfth pass - temperature[11] is assigned value 0


    }

    JOptionPane.showMessageDialog(null, "The temperatures are " + total , "Results",
    JOptionPane.INFORMATION_MESSAGE);
    // prints “the temperatures are 0”



    Basically, you haven't assigned your input values to your array. Then you assign the value 0 to each element of your array and then you print the value of a variable that hasn't changed since it was created.


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    I assigned certain values to 0 because without doing this I get errors in my "total" how can I stop this, and how can I assign values like you said? ... Thank you.


  • Registered Users Posts: 773 ✭✭✭pillphil


    Your first loop takes input from the user and overwrites the value in temp on each iteration. So when it ends, you have discarded each value apart from the final one.

    Then you have a second loop which assigns the value of total (i.e 0) to each element of the array TemperatureArray.

    So by the end of your program, your variables look like this

    temp == 12 (or whatever the final inputted value was)
    total == 0
    TemperatureArray == {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}(because you assign the value in total to each element of the array.

    What you want to do is assign the value of temp to the element of the array on each iteration of the loop.


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    pillphil wrote: »
    Your first loop takes input from the user and overwrites the value in temp on each iteration. So when it ends, you have discarded each value apart from the final one.

    Then you have a second loop which assigns the value of total (i.e 0) to each element of the array TemperatureArray.

    So by the end of your program, your variables look like this

    temp == 12 (or whatever the final inputted value was)
    total == 0
    TemperatureArray == {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}(because you assign the value in total to each element of the array.

    What you want to do is assign the value of temp to the element of the array on each iteration of the loop.

    Ok thank you very much!


  • Registered Users Posts: 773 ✭✭✭pillphil


    That'll populate the array, the second loop will have to be modified as well to add the values.


  • Advertisement
  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    I'm at it now I can't not add the value of total to 0 I still can't get it and the problem is I'm getting frustrated and not thinking logically!! Haha


  • Registered Users Posts: 773 ✭✭✭pillphil


    Post up your code there


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    double temp;
    double total;

    double TemperatureArray [] = new double [12];

    for (int i=0; i<12; i++){
    String tempStr=JOptionPane.showInputDialog("Enter the temperature of each month");
    temp=Double.parseDouble(tempStr);

    temp=TemperatureArray;


    }

    for (int count=0; count<TemperatureArray.length; count++){


    total=TemperatureArray[count];

    total=total+=temp;

    }

    JOptionPane.showMessageDialog(null, "The temperatures are " + total , "Results",
    JOptionPane.INFORMATION_MESSAGE);
    }

    }




    I have temp assigned to hold the values entered by user.. total to count the array. then put total to put both together. I have been messing around trying different things but i always have the error unless temp and total are initialised to 0


  • Registered Users Posts: 773 ✭✭✭pillphil


    Giving them an initial value is fine, you're going to change them later, so it's ok. The issue here is how java assigns variables, it's from right to left. So when you say
    temp=TemperatureArray;
    , you're assigning the value of the current index of tempArray to temp. You're overwriting the value you want (i.e. temp) with the default value of the current index of the array(i.e. 0.0)


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    So I should switch them around ?


  • Advertisement
  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    So I should switch them around ?

    I understand what you mean, so how can I assign my array to values that are not ==0 and therefore display the value I need


  • Registered Users Posts: 773 ✭✭✭pillphil


    You had the right idea. Instead of assigning the current index to temp, you want to assign the value of temp to the current index.
    Temp is just a temporary place to put the value until it's stored in the array.


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    How can I assign temp to the value I tried swapping them around on the = sign


  • Registered Users Posts: 773 ✭✭✭pillphil


    swapping them around the equals sign was correct. That makes your first loop almost correct. Now you have to fix the second loop. Try and explain to me what the code is doing. Tell me what the values of temp, total count and TemperatureArray[count] are before and after the first pass of the loop.


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    double temp=0;
    double total=0; // Assign Variables



    double TemperatureArray [] = new double [12]; // Blank array with 12 spaces

    for (int i=0; i<12; i++){
    String tempStr=JOptionPane.showInputDialog("Enter the temperature of each month");
    temp=Double.parseDouble(tempStr); // Take 12 numbers from user

    TemperatureArray=temp; // Store the 12 numbers in the array and assign to variable temp



    }

    for (int count=0; count<TemperatureArray.length; count++){ // count the lenght of the array


    TemperatureArray[count]=total; // Assign answer to variable total

    temp+=total=total; // Temp combined with total is now the new total

    }

    JOptionPane.showMessageDialog(null, "The temperatures are " + total , "Results",
    JOptionPane.INFORMATION_MESSAGE); // Display the numbers entered in message box
    }

    }


    That is my logic


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Can you explain why this part is wrong, keeps on overwriting with 0


  • Registered Users Posts: 773 ✭✭✭pillphil


    for (int count=0; count<TemperatureArray.length; count++){ // count the lenght of the array


    TemperatureArray[count]=total; // Assign answer to variable total //Java assigns fro right to left, this assigns total to the array

    temp+=total=total; // Temp combined with total is now the new total The full logic of this statement is temp = temp + total = total, you are trying to assign a variable to an expression and then assign that expression to a variable

    }


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    I am getting very confused


  • Registered Users Posts: 773 ✭✭✭pillphil


    In english, what do you want the second loop to do, for example, the first loop :
    Loop through array, assign user input to current index

    So the second loop should:
    Loop through array, ?


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Loop through the array counting the numbers imputed


  • Advertisement
  • Registered Users Posts: 773 ✭✭✭pillphil


    Loop through the array counting the numbers imputed

    Counting or adding?
    If every input is 2 what should the total be?


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    pillphil wrote: »
    Counting or adding?
    If every input is 2 what should the total be?

    Counting , I need the total to display all numbers inputed. Example if user enters 1,2,3,4,5,6,7....12 it should display the temperatures are 1 2 3 4 5 ... 12.


  • Registered Users Posts: 773 ✭✭✭pillphil


    counting will just give you the value 12, which is the length of the array, you don't need a loop to get that value.

    Forget the "the temperatures are" part for a minute. try and get loop 2 to
    loop through array, store current index in a variable, print that variable to the console


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    pillphil wrote: »
    counting will just give you the value 12, which is the length of the array, you don't need a loop to get that value.

    Forget the "the temperatures are" part for a minute. try and get loop 2 to

    So I only need my first loop?


  • Registered Users Posts: 773 ✭✭✭pillphil


    So I only need my first loop?

    You can do it with one loop, but just to keep the logic separate for the minute, keep both loops. loop one populates the array, loop 2 prints to the console.


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Now that I thought about it, if loop 2 only was counting the last number inputed it makes no sense to be included. I now have one loop with fills the array with numbers and am just trying to display them in a single message now, was that a step in the right direction


  • Registered Users Posts: 773 ✭✭✭pillphil


    Up to you, it can be done with only one.


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    After reading back over thr question I think I am just ment to have the message box in the loop to to display the results 12 times then a 13th displaying the highest then a 14th displaying average, so all values are not ment to be on the same box so I think I will be able to finish this now!


Advertisement