The assignment is to Write a Java program (a new NetBeans project called Averager) to accept a set of doubles (more than one) from a Java dialog using a loop. Store these doubles in an array. Compute the average of the set of doubles and output this average to another Java dialog. (see the Sentence Builder problem from last week).
I am having trouble making everything into arrays and getting the average. my code brings up the dialog box for inputs but i do not know what to divide the numbers by since i do not know how many numbers the user will input, and i do not know how to change the numbers inputted into arrays.
import java.awt.Frame;
import javax.swing.JOptionPane;
public class Averager {
public static Frame frame = new Frame();
public static double numbers = 0;
public static void main(String[] args) {
double average = getDoubles();
showAverage(average);
}
public static double getDoubles(){
double average = 0;
while (true)
{
String getNumbers = JOptionPane.showInputDialog(null, "input numbers, leave blank and hit ok to stop.");
if (getNumbers.equals(""))
{
break;
}
double inputs = Double.parseDouble(getNumbers);
if (inputs < 0)
{
break;
}
else
{
average += inputs;
}
}
return average;
}
public static void showAverage(double average){
JOptionPane.showMessageDialog(frame, average , "Your Average", JOptionPane.PLAIN_MESSAGE);
}
}
