Results 1 to 9 of 9
  1. #1
    freebirdcal is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Exclamation Sending Arrays into methods then dividing for an average.

    Hello, I am trying to get the average temperature for the week, from figures entered in by the user, by passing the array into a method then dividing by seves. I am having difficulty getting the syntax down so any help will be greatly appretiated.

    import javax.swing.JOptionPane;

    public class Assign1
    {
    public static void main(String[] args)
    {
    // Declare variables and Array List
    final int tdays = 7;
    String[] days = {"Monday", "Tuesday", "Wenesday", "Thursday", "Friday", "Saturday", "Sunday"};
    String input = null;
    double[] message, message2;
    double message1;
    double[] temp = new double[tdays];

    for (int i = 0; i <days.length ; i++)
    {
    JOptionPane.showInputDialog("Please enter" + days[i] + "temperature in Fahrenheit.");
    temp[i] = Double.parseDouble(input);
    }
    // Calculate Average
    message = calcAverage(temp);

    // Convert Temperature to celcius
    message1 = convertTemp(temp);

    // Display Results
    displayResults(message, message1);
    }

    public static double[] calcAverage(double[] array)
    {
    double[] temp;
    double average;
    average = temp / 7;
    return average;
    }
    public static double convertTemp(double[] array)
    {
    float celcius =((temp-32)*5)/9;
    return celcius;
    }

  2. #2
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    Java Code:
    for (int i = 0; i <days.length ; i++)
    {
       JOptionPane.showInputDialog("Please enter" + days[i] + "temperature in 
          Fahrenheit.");
          temp[i] = Double.parseDouble(input);
    }

    JOptionPane returns an input, but you don't store that input into any variable. You need to have your input variable equal this JOptionPane.


    calcAverage is a mess of syntax errors.
    Java Code:
    public static double[] calcAverage(double[] array)
    {
       double[] temp;
       double average;
       average = temp / 7;
       return average;
    }
    Your formal parameter is 'double[] array' and your actual parameter is 'double[] temp'. This means that when you call calcAverage(temp), 'array' now references the 'temp' array for your use in the calcAverage method. You cannot re-declare temp in the calcAverage method and expect the values to carry over. To access the 'temp' array that you want, you simply use the 'array' array.

    Furthermore, "average = temp / 7;" makes no sense, logically. temp is an array of doubles, not a double itself; it's like saying "yellow / 7". You must loop through each element in the array and sum them up, then divide by 7 outside the loop, and return that.

    Note that the data-type before the method name (for calcAverage, you have double[]) is the return type. You are only returning a double, so the return type should be double, not double[].

  3. #3
    freebirdcal is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Oh I see yes temp / 7 does not make a whole lot of sense. I think I have looped the array properly but it is saying my return type is not initialized.

    public static double calcAverage(double[] array)
    {
    double total = 0; // Initialize the accumalator
    double average; // will hold the average
    for (int index = 0; index < array.length; index++)
    {
    total += array[index];
    average = total / array.length;
    }
    return average;
    }

  4. #4
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    Now the return type is correct but you're storing the returned value into the wrong type.

    Your function call:
    Java Code:
    message = calcAverage(temp);

    calcAverage returns a double. What datatype is message?

    Java Code:
    double[] message, message2;


    Edit: Junky also brought up a couple other points that I missed.
    Last edited by Vase; 02-03-2011 at 10:10 PM.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by freebirdcal View Post
    but it is saying my return type is not initialized.
    Local variables do not have a default value. You must give it an intial value just as the error message says.

    Also, why do you calculate average everytime around the loop. If I gave you five numbers how many times would you calculate the average?

  6. #6
    freebirdcal is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    It is supposed to a double so I can have a place to store the return type that I will eventually have to pass on to a displayResults method.

    import javax.swing.JOptionPane;

    public class Assign1
    {
    public static void main(String[] args)
    {
    // Declare variables and Array List
    final int tdays = 7;
    String[] days = {"Monday", "Tuesday", "Wenesday", "Thursday", "Friday", "Saturday", "Sunday"};
    String input = null;
    double message;
    float message1;
    double[] temp = new double[tdays];

    for (int i = 0; i <days.length ; i++)
    {
    input = JOptionPane.showInputDialog("Please enter" + days[i] + "temperature in Fahrenheit.");
    temp[i] = Double.parseDouble(input);
    }
    // Calculate Average
    meesage = calcAverage(temp);

    // Convert Temperature to celcius
    message1 = convertTemp(temp);

    // Display Results
    displayResults(message, message1);
    }

    public static double calcAverage(double[] array)
    {
    double total = 0; // Initialize the accumalator
    double average; // will hold the average
    for (int index = 0; index < array.length; index++)
    {
    total += array[index];
    average = total / array.length;
    }
    return average;
    }
    public static float convertTemp(double[] array)
    {
    double total = 0; // For accumalator
    for (int index = 0; index < array.length; index++)
    {

    }

    }
    public static void displayResults(double[] memssage,float message1)
    {

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    That solved one problem. What about the other two problems that I mentioned?

  8. #8
    freebirdcal is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    I would only want to average it one time so I should do the averaging after the loop. wow thanks!

  9. #9
    freebirdcal is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Its still a work in progress but I thank you for your support

    import javax.swing.JOptionPane;

    public class Assign1
    {
    public static void main(String[] args)
    {
    // Declare variables and Array List
    final int tdays = 7;
    String[] days = {"Monday", "Tuesday", "Wenesday", "Thursday", "Friday", "Saturday", "Sunday"};
    String input = null;
    double message;
    float message1;
    double[] temp = new double[tdays];

    for (int i = 0; i <days.length ; i++)
    {
    input = JOptionPane.showInputDialog("Please enter" + days[i] + "temperature in Fahrenheit.");
    temp[i] = Double.parseDouble(input);
    }
    // Calculate Average
    message = calcAverage(temp);

    // Convert Temperature to celcius
    message1 = convertTemp(message);

    // Display Results
    displayResults(message, message1);
    }

    public static double calcAverage(double[] array)
    {
    double total = 0; // Initialize the accumalator
    double average; // will hold the average
    for (int index = 0; index < array.length; index++)
    {
    total += array[index];
    }
    average = total / array.length;
    return average;
    }
    public static float convertTemp(double fahrenheit)
    {
    float celcius = ((fahrenheit - 32)*(5))/(9);
    }

Similar Threads

  1. Dividing in Java
    By hawaiifiver in forum New To Java
    Replies: 19
    Last Post: 03-11-2011, 11:09 AM
  2. Using methods and arrays after importing files
    By Jamison5213 in forum New To Java
    Replies: 3
    Last Post: 12-29-2009, 05:49 AM
  3. Arrays and methods
    By namie in forum New To Java
    Replies: 3
    Last Post: 10-05-2009, 09:43 AM
  4. methods, classes, arrays.. oh my!
    By katalyst in forum New To Java
    Replies: 30
    Last Post: 03-30-2009, 12:57 AM
  5. Arrays & Methods
    By TheRocket in forum New To Java
    Replies: 1
    Last Post: 12-10-2008, 07:37 PM

Tags for this Thread

Posting Permissions

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