I just want \"Jan Expenditure\" to appear however only expenditure comes out. How do I do it? Jan is from my monthsArrays. Is there anything missing?
import java
You have a typical +/-1 error in your code.
Your monthsArray has an empty String at position 0 and you take the displayed String from month -1. Simply entering 2 and getting "Jan expenditure (max 10 items)" as a result should have been a good hint.
From the looks of your monthsArray
, the first element is a blank string. Remember that arrays are zero-based, and you asked your user for a month between 1 and 12 (which is fine, because nobody thinks of January as month 0, right?). Later in your code, you do this:
String monthChoice = monthsArray[month - 1];
However, your array actually contains 13 elements, because you added that blank space, so in your case, you're actually safe to simply use the unaltered month
index, like so:
String monthChoice = monthsArray[month];
OR, get rid of the empty string at the beginning of the array, and leave your monthChoice
line as is (depends on how your implementation is supposed to work. That is purely your choice).
Some side notes:
You are presently declaring your monthArray
in the body of the switch statement, which is unnecessary. It would be better to declare it at the top of the method, OR even better, as a constant in the class, like so:
private static final String[] monthsArray = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };
Also, this line:
for (int i=0; i < monthsArray.length; i++)
is useless. I don't know whether this is part of an unfinished solution, however all it is doing is repeating the statement that immediately follows it, which as as far as I can tell, is unnecessary. Basically it is the same as this:
for (int i=0; i < monthsArray.length; i++){
String monthChoice = monthsArray[month - 1];
}
So you should be safe to get rid of it.