Results 1 to 9 of 9
 02032011, 09:41 PM #1Member
 Join Date
 Feb 2011
 Posts
 5
 Rep Power
 0
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 =((temp32)*5)/9;
return celcius;
}
 02032011, 10:05 PM #2Member
 Join Date
 Jan 2011
 Location
 Gainesville, FL
 Posts
 45
 Rep Power
 0
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; }
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 datatype 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[].
 02032011, 10:28 PM #3Member
 Join Date
 Feb 2011
 Posts
 5
 Rep Power
 0
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;
}
 02032011, 11:03 PM #4Member
 Join Date
 Jan 2011
 Location
 Gainesville, FL
 Posts
 45
 Rep Power
 0
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; 02032011 at 11:10 PM.
 02032011, 11:08 PM #5
 02032011, 11:14 PM #6Member
 Join Date
 Feb 2011
 Posts
 5
 Rep Power
 0
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)
{
 02032011, 11:16 PM #7
That solved one problem. What about the other two problems that I mentioned?
 02032011, 11:34 PM #8Member
 Join Date
 Feb 2011
 Posts
 5
 Rep Power
 0
I would only want to average it one time so I should do the averaging after the loop. wow thanks!
 02032011, 11:41 PM #9Member
 Join Date
 Feb 2011
 Posts
 5
 Rep Power
 0
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

Dividing in Java
By hawaiifiver in forum New To JavaReplies: 19Last Post: 03112011, 12:09 PM 
Using methods and arrays after importing files
By Jamison5213 in forum New To JavaReplies: 3Last Post: 12292009, 06:49 AM 
Arrays and methods
By namie in forum New To JavaReplies: 3Last Post: 10052009, 10:43 AM 
methods, classes, arrays.. oh my!
By katalyst in forum New To JavaReplies: 30Last Post: 03302009, 01:57 AM 
Arrays & Methods
By TheRocket in forum New To JavaReplies: 1Last Post: 12102008, 08:37 PM
Bookmarks