Results 1 to 2 of 2

Thread: data to arrays

  1. #1
    Christa11 is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default data to arrays

    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);
    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: data to arrays

    Go through the FAQs section of these forums to learn how to use the code tags so that your code retains its formatting.

    i do not know what to divide the numbers by since i do not know how many numbers the user will input
    Use a variable to count that. Initialize it to 0 and increment it by one each time around the loop.

    i do not know how to change the numbers inputted into arrays
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. I Dont know how to put my data into 2D arrays
    By okokok in forum New To Java
    Replies: 31
    Last Post: 09-22-2011, 09:29 PM
  2. Storing/retrieving arrays in a data structure...
    By pbandjay in forum New To Java
    Replies: 0
    Last Post: 11-03-2010, 12:39 AM
  3. Use of Arrays to store data?
    By moondy in forum New To Java
    Replies: 4
    Last Post: 10-07-2010, 11:11 AM
  4. Input data from file to arrays problem
    By PVL268 in forum New To Java
    Replies: 18
    Last Post: 03-16-2009, 05:05 AM

Posting Permissions

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