Results 1 to 4 of 4
  1. #1
    pds8475 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default adding numbers in an array together

    i need to add all the values together from the array guestNumbers
    any idea's


    Java Code:
     String e1;
           double e;
           for(int i=0; ng.hasNext();i++){
             e1=ng.next();
             e=Double.parseDouble(e1);
             guestNumbers[i]=e;
           }
    
    
    System.out.println(guestNumbers[0]);
    System.out.println(guestNumbers[1]);
    System.out.println(guestNumbers[2]);
    System.out.println(guestNumbers[3]);

    this outputs to the screen as:

    1.0
    2.0
    3.0
    1.0

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi, Sorry What does mean "to add all the values together"?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    pds8475 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    hi i want to add all the elements to a double ie do

    double sum =guestNumber[0]+ guestNumber[1]+ guestNumber[1] and so on untill the end off the arra2

    System.out.println("total = " +sum);

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    It is clear, It is very simple do.
    Java Code:
    public class SummMass {
    
        public static void main(String[] arg) {
            double[] mass = {1.0, 2.3, 4.2, 2.0 , 7.9, 3.3};
            System.out.println("total = " +summ(mass));
        }
    
        private static double summ(double[] mass) {
            double summ = 0;
            for (double value : mass) {
                summ += value;
            }
            return summ;
        }
    }
    Skype: petrarsentev
    http://TrackStudio.com

Similar Threads

  1. Array of random numbers ...
    By shane1987 in forum New To Java
    Replies: 31
    Last Post: 11-14-2010, 10:33 PM
  2. Adding numbers for sales reciept program(Biginner)
    By waklo99 in forum New To Java
    Replies: 10
    Last Post: 03-18-2010, 05:09 AM
  3. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 04:50 AM
  4. Adding numbers in a 2 dimensional array
    By j0shizabeast in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 05:31 AM
  5. Adding numbers in array
    By Shaolin in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 07:30 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
  •