# Minimizing Repetitive Code(Urgent!!!)

static void MonthData()
{
if(months[m]=="Jan"){
for(int i=0;i<10;i++){
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : ");
janItems[i]=input.next();
if(janItems[i].length()==0){
return;
}
else{
System.out.print("Enter amount : \$");
janamount[i]=input.nextDouble();
}System.out.println("");
}
}
if(months[m]=="Feb"){
for(int i=0;i<10;i++){
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : ");
febItems[i]=input.next();
if(febItems[i].length()==0){
return;
}
else{
System.out.print("Enter amount : \$");
febamount[i]=input.nextDouble();
}System.out.println("");
}
}
if(months[m]=="Mar"){
for(int i=0;i<10;i++){
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : ");
marItems[i]=input.next();
if(marItems[i].length()==0){
return;
}
else{
System.out.print("Enter amount : \$");
maramount[i]=input.nextDouble();
}System.out.println("");
}
}
if(months[m]=="Apr"){
for(int i=0;i<10;i++){
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : ");
aprItems[i]=input.next();
if(aprItems[i].length()==0){
return;
}
else{
System.out.print("Enter amount : \$");
apramount[i]=input.nextDouble();
}System.out.println("");
}
}
if(months[m]=="May"){
for(int i=0;i<10;i++){
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : ");
mayItems[i]=input.next();
if(mayItems[i].length()==0){
return;
}
else{
System.out.print("Enter amount : \$");
mayamount[i]=input.nextDouble();
}System.out.println("");
}
}
if(months[m]=="Jun"){
for(int i=0;i<10;i++){
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : ");
junItems[i]=input.next();
if(junItems[i].length()==0){
return;
}
else{
System.out.print("Enter amount : \$");
junamount[i]=input.nextDouble();
}System.out.println("");
}
}
if(months[m]=="Jul"){
for(int i=0;i<10;i++){
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : ");
julItems[i]=input.next();
if(julItems[i].length()==0){
return;
}
else{
System.out.print("Enter amount : \$");
julamount[i]=input.nextDouble();
}System.out.println("");
}
}
if(months[m]=="Aug"){
for(int i=0;i<10;i++){
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : ");
augItems[i]=input.next();
if(augItems[i].length()==0){
return;
}
else{
System.out.print("Enter amount : \$");
augamount[i]=input.nextDouble();
}System.out.println("");
}
}
if(months[m]=="Sep"){
for(int i=0;i<10;i++){
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : ");
sepItems[i]=input.next();
if(sepItems[i].length()==0){
return;
}
else{
System.out.print("Enter amount : \$");
sepamount[i]=input.nextDouble();
}System.out.println("");
}
}
if(months[m]=="Oct"){
for(int i=0;i<10;i++){
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : ");
octItems[i]=input.next();
if(octItems[i].length()==0){
return;
}
else{
System.out.print("Enter amount : \$");
octamount[i]=input.nextDouble();
}System.out.println("");
}
}
if(months[m]=="Nov"){
for(int i=0;i<10;i++){
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : ");
novItems[i]=input.next();
if(novItems[i].length()==0){
return;
}
else{
System.out.print("Enter amount : \$");
novamount[i]=input.nextDouble();
}System.out.println("");
}
}
if(months[m]=="Dec"){
for(int i=0;i<10;i++){
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : ");
decItems[i]=input.next();
if(decItems[i].length()==0){
return;
}
else{
System.out.print("Enter amount : \$");
decamount[i]=input.nextDouble();
}System.out.println("");
}
}
}[code][/code]

Program Requirements:

-Should store monthly expenses for Jan~Dec and display them.

As you all can see, the code is ridiculously repetitive and quite frankly,annoying even for me.I am using the very most [b]basic of skills[/b] in this program,so please forgive me if this code seems too general.
Any help to reduce the number of lines is greatly appreciated!

• you can use the following class. the monthes are called by and index between 0 (for jan) and 11 (for dec). here is the code

[code]import java.io.Console;
import java.util.ArrayList;

// this class holds the item name and the expence
class Item {
String item;
double expence;

public Item(String i, double d) {
this.item = i;
this.expence = d;
}

public String toString() {
return this.item + " for " + this.expence + "\$";
}
}

// this class holds all items in an arraylist
class ItemExpences {
ArrayList item;

public ItemExpences() {
item = new ArrayList();
}

public void addItem(String s, double d) {
}
}

public class Expences {

int month = 0;

Console reader = System.console();

ItemExpences[] months = new ItemExpences[12];

// for each month and instace of Expences is initialized
public Expences() {
for (int i = 0; i < 12; i++) {
// the array expences[] contains two arraylists
months[i] = new ItemExpences();
// for demostration examples two items are added to Jan and Feb
if (i == 0) {
months[i].addItem("JanItem" + i, 10.5);
months[i].addItem("JanItem" + i + 1, 48.2);
} else if (i == 1) {
months[i].addItem("FebItem" + i, 22.5);
months[i].addItem("FebItem" + i + 2, 25.9);
}
}
}

public void readExpence() {
int m;
String s;
double d;

do {
System.out.print("Enter the month of the expence (0-11): ");
} while (m < 0 || m > 12);

System.out.println("readed month " + m);

// only one item is readed, but you can use a while loop so
// the user can choose when to stop
int size = 1;
for (int i = 0; i < size; i++) {
System.out.print("Enter item " + (i + 1)
+ " : ");
if (s.length() > 0) {
System.out.print("Enter amount : \$");
}
}
}

public void printAll() {
for (int i=0; i < months.length; i++) {
System.out.println("Expences for month: " + i);
for (int j=0; j < months[i].item.size(); j++) {
System.out.println(months[i].item.get(j));
}
}
}
}
[/code]

for a small test use this class

[code]public class ExpencesExample {
public static void main(String[] args) {
Expences exp = new Expences();
exp.printAll();
}
}
[/code]

don't start this code inside an ide, because some ide are not able to handle the console. as you can see all monthes and all items are handled by the same code. if you have questions, let me know.
• Thanks for the tremendous help.Much appreciated.I will now see how I can implement your code into mine without my lecturer thinking I copied from someone else.Anyways, here's the scanned, complete requirements list of the program(if you want to look at it).

Thanks again.
• great. it should'n be very difficult to adapt the code so the output is like in the pdf. if you have further questions, write me at j2se@gmx.ch