字符串无法转换为 java 错误:尝试将字符串转换为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21444844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:45:17  来源:igfitidea点击:

String cannot be cast to java error: Trying to convert string into integer

java

提问by user3250995

When trying to add years from a combobox this error shows:

尝试从组合框中添加年份时,此错误显示:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

线程“AWT-EventQueue-0”中的异常 java.lang.ClassCastException:java.lang.String 无法转换为 java.lang.Integer

at LibraryFrame$addListener.actionPerformed(LibraryFrame.java:103)

在 LibraryFrame$addListener.actionPerformed(LibraryFrame.java:103)

The program highlights the 'Integer anInteger = (Integer) selected;' line as the problem?

程序高亮显示'Integer anInteger = (Integer) selected;' 线路问题?

class addListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
         Object selected = yearCombo.getSelectedItem(); 

        if(e.getSource() == button)
        {
            System.out.println("Add button clicked!");
            String t = title.getText();
            Integer anInteger = (Integer) selected;
            int y = anInteger;
            int q = Integer.parseInt(quantity.getText());
            library.addItem(new LibraryItem(t,y,q));
            library.printLibrary();

            System.out.println("Thank You!");
        }
    }
}

Heres the code where yearCombo is populated:

这是填充 yearCombo 的代码:

label1 = new JLabel("Year");
    yearCombo = new JComboBox();
    ArrayList<Integer> countYear = new ArrayList<Integer>();
    for(int y=1800; y<=2014; y++)
    {
        countYear.add(y);
    }
    for(int x=0; x < countYear.size(); x++)
    {
        yearCombo.addItem((countYear.get(x) + " "));            

    }               

CODE NOW THROWING A NUMBER FORMAT EXCEPTION after changing

代码现在在更改后抛出数字格式异常

    Integer anInteger = (Integer) selected;

to

    Integer anInteger = Integer.parseInt(selected.toString());

采纳答案by Ingo

This:

这个:

yearCombo.addItem((countYear.get(x) + " "));
//                                     ^--------------- not numeric

results in a string that cannot be converted to a number, no matter what countYear.get(x)gives you.

导致无法转换为数字的字符串,无论countYear.get(x)给您什么。

Do you really need to add that space there?

你真的需要在那里添加那个空间吗?

回答by Dima

change

改变

 Integer anInteger = (Integer) selected;

to

 Integer anInteger = Integer.parseInt(selected.toString());

as the error says, String and integer are completely different classes,

正如错误所说,字符串和整数是完全不同的类,

you can't cast String to Integer as you can't use television as a car.

您不能将 String 转换为 Integer,因为您不能将电视用作汽车。

edit:

编辑:

NumberFormatExceptionmeans that the object selectedis not a number. if you have a string like "hello", what do you expect it to be as an integer?

NumberFormatException表示对象selected不是数字。如果你有一个像 的字符串"hello",你希望它是一个整数吗?