0

I am making an expense tracker program for an assessment project, but the thing is, the question sheet is too vague. One of the problems is that, I am not sure if I should just stick to a one-dimensional array or a multi-dimensional one.

The whole idea is that; a user selects a month and it will prompt an option that allows user to assign the expense to that month and to a certain item. It would look some thing like this:

Enter month (1 for Jan - 12 for Dec): 1

Jan expenditure(max 10 items) Enter item 1 (Press ENTER to exit): fast food

Enter amount : $10

What kind of dimensional array should I used? It seems that as I progress into the different dimension of arrays, it kinda like open up a can of worms.

So far, I have gotten this:

  int m = 12;
int exp = 10;

int[][] month = new int [m][exp];


public void data(){


}



public void monthlyExp(){
    String[] mth = {"Jan", "Feb", "Mar", "Apr",
            "May", "Jun", "Jul", "Aug", "Sep",
            "Oct","Nov","Dec"
    };

    System.out.print("Enter month > ");
    int mon = input.nextInt();

    for (int i = 0; i < month.length; i++){
        System.out.println();
        if (i == (mon-1)){
            System.out.println(mth[i] + " expenditure <max 10 items>");
            while (true){
                for (int h = 0; h < exp; h++);
                System.out.print("Enter item " + (h + 1) + "(PRESS ENTER TO EXIT)");
            }

2 Answers2

0

One approach is to create a POJO class with attributes for the month, expense, and description of expense, and have a single array of your POJO objects. If you find the confusing, try using parallel arrays or better yet ArrayLists. That is, have a separate array for each of month, expense, and description. Each time you have a new entry in your ledger, add an element to each of the arrays, using the same index, then increment the index for the next entry.

Teresa Carrigan
  • 668
  • 8
  • 14
0
public static void main(String[] args) {
    int[] arr = new int[5];
    int[][] newArr = new int[5][5];
    // arr = newArr;    compile time error 
    // newArr = arr;    compile time error
    newArr[0]=arr;    // valid. But this is not preferred and is considered a bad practice. You might run into trouble later on with this little piece of code. So use Objcts/collections whereever possible instead of arrays.

    for(int i=0;i<5;i++)
    {
        newArr[0][i]=i;  // ArrayIndexOutOfBoundsException at runtime (arr has size of 5 remember?). So, be wary of such assignments. Everything seems fine until it breaks. 
    }
}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104