Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default for loop with arrays help

    Okay, here's the thing. I am attempting to make a program that takes the temperatures from the 7 days of the week from the user. I know I could easily do it as 7 separate JOptionPane.showInputDialog(), but I would like to see if I can make it work as a for loop and change the day each iteration of the loop. Right now I have it compiled to work and store the data in the proper arrays, but all it asks for is Monday's temp 7 times. Any help or suggestions would be awesome. Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by killasaurus tex View Post
    Okay, here's the thing. I am attempting to make a program that takes the temperatures from the 7 days of the week from the user. I know I could easily do it as 7 separate JOptionPane.showInputDialog(), but I would like to see if I can make it work as a for loop and change the day each iteration of the loop. Right now I have it compiled to work and store the data in the proper arrays, but all it asks for is Monday's temp 7 times. Any help or suggestions would be awesome. Thanks.
    Show the code, and we'll likely be able to help spot your error. If you do post code, please use code tags though. The link in my signature immediately below will show you how.

  3. #3
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
    package tempavgerage;
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    import java.io.*;
    
    
    public class TempAverage {
    
    
        public static void main(String[] args) {
    
            DecimalFormat twoSpace = new DecimalFormat("#0.##");
    
            //Declare variables
            double mon, tue, wed, thur, fri, sat, sun;
            double averageFar = 0.0;
            double averageCel = 0.0;
            double[] temps = new double[7];
    
    NOTE: This is what I'm having trouble with:
    
            //Get temperatures for monday-friday
            for (double index = 0; index < temps.length; index++)
            {
            temps[0] = Double.parseDouble(JOptionPane.showInputDialog(
                    null, "Enter the temperature from Monday: ", "Temp Calculator",
                    JOptionPane.QUESTION_MESSAGE));
            
            }
    /NOTE
    
            averageFar = calcAverage(temps);
            averageCel = calcCel(averageFar);
    
            JOptionPane.showMessageDialog(
                    null, "The average temperature is " + twoSpace.format(averageFar)
                    + " degrees Fahrenheit, " +
                    "which was " + twoSpace.format(averageCel) + " degrees Celsius.");
        }
    
        //method for calculating the average in Fahrenheit
        public static double calcAverage(double[] tmps)
        {
            double total = 0.0;
            double average = 0.0;
    
            for (int arrayIndex = 0; arrayIndex < tmps.length; arrayIndex++)
            {
                total+=tmps[arrayIndex];
            }
    
            average = total/tmps.length;
            return average;
        }
    
        //method for calculating the average in Celcius
        public static double calcCel(double averageFar)
        {
            double average = 0.0;
    
            average = (((averageFar-32)*(5))/9);
    
            return average;
        }
    
    }
    I know that I have it set up to just say Monday right now, but I'm trying to figure out what the most effective way to get it to change the days of the week is while still saving the input to the array.
    *Note also just noticed I'm saving the value input into the 0 slot all seven times, so I guess I might need some help with that as well.
    Last edited by killasaurus tex; 02-27-2011 at 11:04 PM.

  4. #4
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    I think your for loop should be a int instead of a double.

    Java Code:
            for ([COLOR="Red"]int[/COLOR] index = 0; index < temps.length; index++)
            {
            temps[[COLOR="Red"]index[/COLOR]] = Double.parseDouble(JOptionPane.showInputDialog(
                    null, "Enter the temperature from Monday: ", "Temp Calculator",
                    JOptionPane.QUESTION_MESSAGE));
            }
    An array takes an integer in the brackets to access the index.
    Last edited by AcousticBruce; 02-27-2011 at 11:07 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One way: create an array of String filled with day of the week Strings. Create your prompt String inside the for loop using this array and the for loop index.

  6. #6
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Thanks for the input. I did what you both said and it worked perfectly. I knew that I needed an int, I don't know why I couldn't remember to put that.

Similar Threads

  1. Scanner, while loop and sorting arrays/string?
    By RSYR in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 07:13 PM
  2. Replies: 8
    Last Post: 03-10-2011, 04:16 AM
  3. Adding Arrays and Enhanced For Loop into program.
    By vinyacam in forum New To Java
    Replies: 1
    Last Post: 05-10-2010, 07:28 AM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  5. loop problem-arrays
    By ester in forum New To Java
    Replies: 0
    Last Post: 02-02-2010, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •