Results 1 to 7 of 7
  1. #1
    mrparker2 is offline Newbies
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Very basic java problem

    I'm brand new to programming so this may seem stupid. I have a problem where i've made an array of ten double numbers in one class and i have to make a method in another class that adds all of these numbers together using a "for" loop and utilizing the array's length number. I've searched and searched for how to do this and i cannot figure out how it's done. i can get the numbers to print out but i cannot get them to add. Here's what i have:

    Java Code:
     public void sumDoubles(double[] f) {
        double sum = 0;
        for(int g = 0;g<f.length;g++){
        sum +=f[g];
        System.out.println(sum);
      }
    any help would be great thanks
    Last edited by JosAH; 05-26-2011 at 06:13 PM. Reason: added [code] ... [/code] tags around the program code

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    You're adding those numbers correctly but you're not doing anything with the sum, i.e. aren't you supposed to return it from the method?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    mrparker2 is offline Newbies
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    If i'm following correctly, then yes if the numbers are adding correctly i need the method to give the sum when ran. I just don't know how to do this exactly

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Look at this in the Java Tutorial:
    http://download.oracle.com/javase/tu...turnvalue.html

  5. #5
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    try this. you will have to clean up the decimal places though, but you can use a NumberFormat method for that:

    public class SumAdd
    {
    public static void main(String[] args)
    {
    double[] tenNumbers = {1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1, 10.1};
    double finalSum;
    new SumAdd();
    finalSum = SumAdd(tenNumbers);
    System.out.println(finalSum);


    }

    public void SumAdd()
    {
    }
    public static double SumAdd(double[] getArray)
    {
    double sum = 0;

    for (int i = 0; i < getArray.length; i ++)
    {
    sum += getArray[i];

    }

    return sum;
    }
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Joel View Post
    try this. you will have to clean up the decimal places though, but you can use a NumberFormat method for that:
    Please compile and test your code for correctness before you post it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    Jos-
    compiles and runs just fine, but I should have cleaned up the open/close brackets to look more conventional. thanks...

Similar Threads

  1. Replies: 1
    Last Post: 11-18-2010, 08:17 PM
  2. Basic Java GUI problem, JPanel not resizing.
    By Cemi in forum New To Java
    Replies: 10
    Last Post: 04-11-2010, 01:20 AM
  3. Triangles java problem (basic help)
    By adz06 in forum New To Java
    Replies: 5
    Last Post: 10-31-2009, 06:58 PM
  4. What am I doing wrong in this basic problem
    By Blaedel in forum New To Java
    Replies: 10
    Last Post: 10-11-2009, 07:45 PM
  5. [SOLVED] very basic java problem
    By sales1 in forum New To Java
    Replies: 13
    Last Post: 08-20-2008, 08:33 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
  •