Cannot format given Object as a Number ComboBox

江枫思渺然 提交于 2019-12-24 11:37:15

问题


How can I set an integer number into double? Let's say I have a comboBox named day, and it has 1,2,,3 elements. I want to set it as two decimal point. If user select 1, it will become 1.00. How can I do that?

public void actionPerformed(ActionEvent e){
                        String a=(String)comboBox.getSelectedItem();
                        //Integer b=(comboBox_1.getSelectedIndex()+1);
                        int day=(Integer)comboBox_2.getSelectedItem();
                        double bo;
                        DecimalFormat df = new DecimalFormat("#.##");      
                        bo= Double.valueOf(df.format(day));

Error I get

java.lang.IllegalArgumentException: Cannot format given Object as a Number
    at java.text.DecimalFormat.format(DecimalFormat.java:507)
    at java.text.Format.format(Format.java:157)
    at gui.User.<init>(User.java:105)
    at gui.User$1.run(User.java:49)

回答1:


Check this

public void actionPerformed(ActionEvent e){
                    String a=comboBox.getSelectedItem().toString();
                    //Integer b=(comboBox_1.getSelectedIndex()+1);
                    int day=Integer.ParseInt(comboBox_2.getSelectedItem().toString());
                    double bo;
                    DecimalFormat df = new DecimalFormat("#.##");      
                    bo=  Double.parseDouble(df.format(day));


来源:https://stackoverflow.com/questions/32174400/cannot-format-given-object-as-a-number-combobox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!