Solved

Adding columns in a jagged array in Java

Posted on 2013-11-10
5
934 Views
Last Modified: 2013-11-10
Good evening;
   I am creating a java file that uses jagged arrays to store the amount that someone has spent for each day of the week.  I have my program adding the total amount spent in the week and also the average spent per day, however when I am trying to calculate the amount spent per day I am running into issues.  Here is my array and my for loop to add the columns and display the values:

        // implement a multidimensional array to represent 1 week
            // for each day of the week (0-6) implement the number of times 
                // money was spent as well as amount spent
        int[][] weekArray = {
            {7, 17, 23},
            {5, 10, 6, 8, 25},
            {13},
            {2, 7, 9, 11, 31, 7, 4},
            {5, 6, 2, 18},
            {},
            {10,22, 8}
        };
        
        // total spent per day
        // sunday
        for (int column = 0; column < weekArray[0].length; column++) {
            int sundayTotal = 0;
            for (int row = 0; row < weekArray.length; row++) {
                sundayTotal += weekArray[row][column];
            }
            System.out.println("The sum for Sunday is $" + sundayTotal +);
        }

Open in new window


I am receiving an error for an array index out of bounds.  I am unable to see where I am accessing an index that does not exist here.  I know this is a simple loop to add the values in each column, but I am stumped as to where this error is coming from, any nudges in the right direction?
0
Comment
Question by:Autkast
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 100 total points
ID: 39637594
You are getting that error because I believe you are adding along the wrong dimension. With the code above, on the first iteration of the outer loop you are attempting to add the 7, 5, 13, 2, 5, ?, and the 10 together. Notice the ? in what I just wrote... Your code is trying to access the first element of the 6th array (line 10) but that array is empty so there is NO first element and you get the error that you are seeing.

You need to change the order of the dimensions that you are iterating, ie. start by swapping the two "for" loops that you have.
0
 

Author Comment

by:Autkast
ID: 39637613
ah ha!  And once I have it switched, as opposed to having the 0 in the "column" loop, I use the length of the "row"!  Great!  That was so easy once you opened my eyes, thank you so much!
0
 

Author Closing Comment

by:Autkast
ID: 39637614
Thank you, provided direction without giving the full answer, allowing me a chance to learn from my question.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39637653
Not a problem, glad that I could help! :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Chrome and Firefox Java 5 88
Java 8 to Java 6 8 43
passing enum to a method 4 50
Java - accessing a parent class member when another class inherits from the parent class 9 56
Introduction This article discusses the Chain of Responsibility pattern, explaining What it is;Why it is; andHow it is At the end of this article, I hope you will be able to describe the use and benefits of Chain of Responsibility.  Backgrou…
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question