Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: java: adding array elements

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java: adding array elements

    Dear all,
    I wish to write a Java application program and an applet program that find the sum of all the integers in the array shown below and print them out together with the sum as below:

    int[ ][ ]MyArray={{2, 3, 4, 5 }, {6, 7,8}, {9, 10}, {11}}

    expected output:

    2 3 4 5
    6 7 8
    9 10
    11
    Total = 28 20 12 5

    (eg col1 6+2+9+11=28, col2: 3+7+10=20, col3: 8+4=12, col4: 5)

    That is, the program arranges all first elements in one column, all second elements in another vertical column, etc and displays as output as shown above.. Then it sums them up the corresponding elements and displays them as Total = 24 17 10 4 as above. When I try, am getting wrong values for the sum and am unable to display the elements in vertical column.

    Here is my code for the application program part:
    public class AssignmtOneQuiz1 {

    public static void main(String args[]) {
    int sum = 0;


    int[ ][ ]MyArray={{2, 3, 4, 5 }, {6, 7,8}, {9, 10}, {11}};

    System.out.print("Total = ");
    for(int i=0; i<MyArray.length; i++)
    {

    for(int j=0; j<MyArray[i].length; j++)
    sum += MyArray[i][j];
    System.out.print(sum + " ");
    }


    }

    Butt it's not giving the required results. lease assist.
    Thanks

    }


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    50
    My Mood
    Fine
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: java: adding array elements

    Everytime you're outputting the "sum", which accumulates all your terms, so this is why you get higher and higher values everytime. At some point, you need to stop accumulating the past values, and start with the new ones.

    Got it?

    application context
    Last edited by daniel.j2ee; December 13th, 2011 at 05:02 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java: adding array elements

    Hi Daniel. Got it.
    Infact the code now works perfect.

    However, it only works as an application. How can I write the same code such that it is done in an applet this time and not application since application is ok?

    please help

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java: adding array elements

    You need to read about how to write an applet. Go to this site and Find applet:
    The Really Big Index

    When you have code that executes as an applet, you can add your logic to it. One change you'll need to make is that the output should go to a field in the GUI and not be via println.
    The GUI code will be the same as for an application, so I'd suggest that you change your program to output the results to a GUI field and then use that same code in the applet.

Similar Threads

  1. Help with ArrayList ( Adding certain elements together)
    By williamsant in forum Collections and Generics
    Replies: 13
    Last Post: September 27th, 2011, 09:32 AM
  2. Comparing similar elements in an array
    By FJIW in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 25th, 2011, 10:22 AM
  3. Comparing elements of an arrayList with an array?
    By DudeJericho in forum Collections and Generics
    Replies: 2
    Last Post: April 21st, 2011, 12:39 PM
  4. Adding array elements inside a loop
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2010, 09:48 PM
  5. Method Adding elements to an array with certain restrictions
    By Newoor in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2009, 11:13 AM