Hi, I'm currently working on a GUI programme that caluculates the amount of days in a month through user input.
However I've become stuck where the programme crashes upon entering the month number.
I believe that I may have incorrectly chosen my instance variables but I am unsure of which ones.
Any help is greatly appreciated.
here's what I have done so far.
String monthName[] = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
String monthString;
int month = 8;
switch (month) {
case 1:
monthString = "January";
break;
case 2:
monthString = "February";
break;
case 3:
monthString = "Mrach";
break;
case 4:
monthString = "April";
break;
case 5:
monthString = "May";
break;
case 6:
monthString = "June";
break;
case 7:
monthString = "July";
break;
case 8:
monthString = "August";
break;
case 9:
monthString = "September";
break;
case 10:
monthString = "October";
break;
case 11:
monthString = "November";
break;
case 12:
monthString = "Decemer";
break;
default:
monthString = "Invlid Month";
break;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
int numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
numDays = 29;
break;
default:
System.out.println("Invalid month.");
break;
}
String[] result =monthName;
resultLabel.setText(String.format("Result: %.2f", result));
}
private void calculatebuttonActionPerformed(java.awt.event.ActionEvent evt) {
String monthName[] = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
String monthString;
int month = 8;
switch (month) {
case 1:
monthString = "January";
break;
case 2:
monthString = "February";
break;
case 3:
monthString = "Mrach";
break;
case 4:
monthString = "April";
break;
case 5:
monthString = "May";
break;
case 6:
monthString = "June";
break;
case 7:
monthString = "July";
break;
case 8:
monthString = "August";
break;
case 9:
monthString = "September";
break;
case 10:
monthString = "October";
break;
case 11:
monthString = "November";
break;
case 12:
monthString = "Decemer";
break;
default:
monthString = "Invlid Month";
break;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
int numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
numDays = 29;
break;
default:
System.out.println("Invalid month.");
break;
}
String[] result =monthName;
resultLabel.setText(String.format("Result: %.2f", result));
}
The above code is for the calculte button, the result of which will be outputted to the
result label