Results 1 to 10 of 10
  1. #1
    Lene90 is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default adding integers of an array

    Hi

    I'm trying to write a simple program that takes an unlimited number of integers from the command line and then adds them together.


    Thanks

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

    Default

    Hi.
    We happy for you. You did not point your problems or questions. :)
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Cool, what have you tried? Are you getting errors? Have you started? If not, try erring somethig up and come here with any problems. If you get errors post the exact errors(no paraphrasing), if you post code use code tags.

    [code]
    YOUR CODE HERE
    [/code]

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    For that project, you need to have a rough idea of how to handle arrays (the input frmo the command line is all sent as an array), make a for-loop to go through the array, and make use of the Integer-class to make them addable. Have a look at Integer (Java Platform SE 6) for the latter.

  5. #5
    Lene90 is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    public class one {

    public static void main(String [] args){

    int a = Integer.parseInt(args[0]);
    int b = Integer.parseInt(args[1]);
    int c = Integer.parseInt(args[2]);

    int sum = 0;

    for(int i=0; i < args.length; i++)
    sum = a+b+c;
    System.out.println(sum);
    }
    }

    This is what I have written so far, however it has to take a fixed number of inputs from the command line. I'm not sure how to change this so that the user can enter an unlimited number of inputs.

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    You're actually very much on the right track. However: Why do you loop through the entire args[] array, when all you do in that loop is to add the three sums together?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You want to loop through the input array and keep a running total as you loop through.

  8. #8
    Lene90 is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Hey got an answer:

    public class one {

    public static void main(String[] args) {

    double sum = 0.0;

    for (int i = 0; i < args.length; i++) {
    sum += Integer.parseInt(args[i]);
    }

    System.out.println(sum);

    }

    }

    Thanks very much for your help :)

  9. #9
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    That'd do the trick, yes. On a small side-note, I would personally change sum to type int instead of double though. You only ever add integers to it. after all.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your welcome, please mark your thread solved with the thread tools at the top.


    ps: you can safely make the sum into an integer here since you won't be dividing or anything, if you do division then you wanna switch sum to double.

Similar Threads

  1. how do you add up integers in an array?
    By shazakala in forum New To Java
    Replies: 7
    Last Post: 04-19-2011, 10:32 AM
  2. Printing repeating integers in a given array?
    By baumboards in forum New To Java
    Replies: 7
    Last Post: 01-24-2011, 07:08 PM
  3. adding integers in JTable
    By khanzaman in forum AWT / Swing
    Replies: 2
    Last Post: 06-14-2010, 09:12 PM
  4. file input: array of integers
    By hannes in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 03:44 PM
  5. Error if array contains duplicate integers
    By lithium002 in forum New To Java
    Replies: 4
    Last Post: 12-05-2009, 08:58 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
  •