Question : Java Swing Days in Month

Could someone help me with this switch statement. It receives  the input from a text field and if the user enter a month of the year if displays the month..
All that happens in this code is that every month is displayed with 31 days..
Also I would like the label to be set to invalid month when the user does not enter a month.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
private void showdaysButtonActionPerformed(java.awt.event.ActionEvent evt) {       
  for ( int month = 1; month <= 12; month++ ){
            int year=0;
            int days = 0;
            String monthName = monthTextField.getText();
            switch (month) {
                case 1: monthName.equalsIgnoreCase ("January");
                    days = 31;
                break;
                case 2: monthName.equalsIgnoreCase("february");
                    if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
                        days = 29;
                        } else {
                            days = 28;
                            }
                break;
                case 3: monthName.equalsIgnoreCase("March");
                    days = 31;
                break;
                case 4: monthName.equalsIgnoreCase("April");
                    days = 30;
                break;
                case 5: monthName.equalsIgnoreCase("May");
                    days = 31;
                break;
                case 6: monthName.equalsIgnoreCase("June");
                    days = 30;
                break;
                case 7: monthName.equalsIgnoreCase("July");
                    days = 31;
                break;
                case 8: monthName.equalsIgnoreCase("August");
                    days = 31;
                break;
                case 9: monthName.equalsIgnoreCase("September");
                    days = 30;
                break;
                case 10: monthName.equalsIgnoreCase("October");
                    days = 31;
                break;
                case 11: monthName.equalsIgnoreCase("november") ;
                    days = 30;
                break;
                case 12: monthName.equalsIgnoreCase("December");
                    days = 31;
                break;
                daysLabel.setText("Invalid Month"); System.exit(0);
                break;
 
            }
  daysLabel.setText(days +" Days");
 
 
 
public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new daysinyears().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify                     
    private javax.swing.JLabel daysLabel;
    private javax.swing.JLabel monthLabel;
    private javax.swing.JTextField monthTextField;
    private javax.swing.JButton showdaysButton;
    // End of variables declaration

Answer : Java Swing Days in Month

1.I suppose you didn't post your original code. The code above does not compile because of the lines 47 and 48 which are unreacheable. However, the only reason for the behavior you described could be the missing of "break" statements.

2.In order to use Calendar class you have to include in the import section either:

import java.util.Calendar;

or

import java.util.*;
Random Solutions  
 
programming4us programming4us