0

So I have this program that reads a Java textfile and prints out the numbers, which are electric bills. Then it finds out the maximum and prints that out along with what month the max came from. My teacher looks for efficiency of code, and I was wondering if there was an easier or possible way to factor the months of the year, instead of using an if else statement. I read about it and I'm pretty sure that Java has the months stored somewhere, but I'm not sure how to get to it. (I'm just beginning to learn java so please use basic terms/code)

My code is:

  if (count == 0)
     System.out.println("File had no numbers");
  else {
     String month="";
     if (count==1) month="January";
     else if (finalcount==2) month="February";
     else if (finalcount==3) month="March";
     else if (finalcount==4) month="April";
     else if (finalcount==5) month="May";
     else if (finalcount==6) month="June";
     else if (finalcount==7) month="July";
     else if (finalcount==8) month="August";
     else if (finalcount==9) month="September";
     else if (finalcount==10) month="October";
     else if (finalcount==11) month="November";
     else if (finalcount==12) month="December";
     System.out.println("Largest Bill: "+max+ " (" +month+")"); 
     System.out.println("Total Yearly Sum: $"+((int)sum*100)/100.0);    
  }

Thanks!

Run9435
  • 21
  • 1
  • 5

2 Answers2

0

The simplest way would be to use an array storing the months, create an array like so:

String months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

Once you have done that, the code you have shown us can be changed to the following:

if (count > 0) {
    month = months[count-1];
    System.out.println("Largest Bill: "+ max + " (" + month + ")"); 
    System.out.println("Total Yearly Sum: $" + ((int)sum*100)/100.0); 
}
else {
    System.out.println("File had no numbers");
}

As other has said, you can use the built in Calendar class, but it is not as simple as this for beginners.

Shadow
  • 3,926
  • 5
  • 20
  • 41
0

I'm not sure why you changed your variable from count to finalcount. As far as another way to do the problem could be a switch statement (if you wanted to execute it in a similar manner), an array, or using Java's Calendar. An easy way is this:

public static String getMonth(int count) { //using the same variable you used
    //I'm going to abbreviate for sake of finishing this faster
    String answer[] = {"File had no numbers","Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"};
    return answer[count];
}// all this will do what you did.

To Use this you would just call it like any other method and pass your 'count' variable.

Brian O.
  • 1
  • 1
  • 6
  • I know this is pretty much the same as the other answer from @Shadow, I pressed the Submit button at pretty much the same time. Both do the same thing in Slightly different ways. Of course, in my solution there is still more to be done you have to call the method and output it etc. – Brian O. Feb 16 '16 at 00:23
  • Also, the above answer in processing is technically more efficient, since method calls take longer than an if statement. In this code, and on today's machines the difference is pretty negligible. – Brian O. Feb 16 '16 at 00:28