I'm new to Java and have just learnt a little on Arrays. However I'm not very sure how I should apply them.

I have a main method which has a switch case. If the user chooses 1, it will prompt the user to enter the daily expenses for a week. If the user chooses 2, it will show the total amount of expenses for the week. I've decided to use arrays to store the the amount for everyday.

here's my code.

double[] monArray= new double[10];
double[] tuesArray= new double[10];
double[] wedArray= new double[10];
double[] thursArray= new double[10];
double[] friArray= new double[10];
double[] satArray= new double[10];
double[] sunArray= new double[10];

for(______________________________)
{               
System.out.print("Enter Mon Charges : $");
monArray[n]= CspInput.readDouble();

System.out.print("Enter Tues Charges : $");
tuesArray[n]= CspInput.readDouble();

System.out.print("Enter Wed Charges : $");
wedArray[n]= CspInput.readDouble();

System.out.print("Enter Thurs Charges : $");
thursArray[n]= CspInput.readDouble();

System.out.print("Enter Fri Charges : $");
friArray[n]= CspInput.readDouble();

System.out.print("Enter Sat Charges : $");
satArray[n]= CspInput.readDouble();

System.out.print("Enter Sun Charges : $");
sunArray[n]= CspInput.readDouble();

After the user entered all the amount for week one, he will return to the choice menu. Then, when the user chooses 1 again and entered the new value for the faily expenses, it will be started as week 2, which will be displayed in choice 2 where it shoows the total amount for all the weeks.

So, what should I place inside the for loop so that the amount for the daily expenses will be stored in the arrays and not be over-written when the user chooses 1 and enter the daily expenses again?

And how do I calculated the total expenses for the week?

It really seems like you should use a 2D array for this. You don't want a separate array for each day. The way you have it will make it hard to sum the entire thing. I would do something like:

double days[][] = new double[7][10];

Then if you want to total its just:

for(int day=0;day<7;day++)
{
for(int exp=0;exp<10;exp++)
{
total = total + expenses[day][exp];
}
}

Then when you add the want to add to each day, you need to loop over the expenses for that day and find the first non-zero element, thats where you add the expense. Of course if you hit 10 for a day, you can't fit anymore expenses in.

Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.