线程“awt-eventqueue-0”java.lang.numberformatexception中的异常:对于输入字符串:“select month”

nnt7mjpx  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(340)

我正试图用JavaSwing开发一个日历应用程序。我有3个jComboBox,您可以在其中选择年、月和日,每次尝试选择年时,我都会遇到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Select Month"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:652)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at calendar.Show$1.itemStateChanged(Show.java:315)
    at java.desktop/javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1227)
    at java.desktop/javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1279)
    at java.desktop/javax.swing.JComboBox.contentsChanged(JComboBox.java:1335)
    at java.desktop/javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:123)
    //......//

我想问题出在我代码的这一部分:

jcbyear3.addItemListener (new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent arg0) {
                int y = Integer.parseInt(String.valueOf(arg0.getItem())); 
                //String mont= String.valueOf(monthjcb.getSelectedItem());

                String monthin = String.valueOf(monthjcb.getSelectedItem());
                String mont = monthin == null || monthin.isEmpty() || monthin.equals("Select Month") ? "January" : monthin;

                //int d=Integer.parseInt(String.valueOf(dayjcb.getSelectedItem()));

                String dayin = String.valueOf(monthjcb.getSelectedItem());
                int d = dayin == null || dayin.isEmpty() || dayin.equals("Select Day") ? 1 :Integer.parseInt(dayin) ;

                int m=0;
                if(mont.equals("January")) {
                    m=1;
                }
                else if(mont.equals("February")) {
                    m=2;
                }
                //.....//

                String[] h=store.search(y, m, d);
                if(h[0].equals("0"))
                    dayarea.setText("No events found");
                else if(h[0].equals("1")){
                    dayarea.setText(h[1]);
                }
                else if(h[0].equals("2")){
                    dayarea.setText(h[1]+"\n"+h[2]);
                }
                else if(h[0].equals("3")){
                    dayarea.setText(h[1]+"\n"+h[2]+"\n"+h[3]);
                }
                else {
                    dayarea.setText(h[1]+"\n"+h[2]+"\n"+h[3]+"\n"+h[4]+"\n"+h[5]);
                }

            }
        });

有人能帮我纠正一下我的问题吗?

wmomyfyw

wmomyfyw1#

这条线

String monthin = String.valueOf(monthjcb.getSelectedItem());

他会还给你的 "Select Month" 然后在你再次要求同样的东西的那一天(即。, monthjcb.getSelectedItem() ):

String dayin = String.valueOf(monthjcb.getSelectedItem());

所以dayin= "Select Month" 正因为如此,你将在有条件的情况下达到:

int d = dayin == null || dayin.isEmpty() || dayin.equals("Select Day") ? 1 :Integer.parseInt(dayin) ;

这部分 Integer.parseInt(dayin) 导致 java.lang.NumberFormatException: For input string: “Select Month” 因为您试图从不包含编码的数值的字符串中提取整数。
根据你的评论,我想说:

String dayin = String.valueOf(monthjcb.getSelectedItem());

你可能想要:

String dayin = String.valueOf(dayjcb.getSelectedItem();

相关问题