简体   繁体   English

据我所知,showinputdialog 返回一个字符串类型的 output 但它给了我一个错误,它无法将 object 转换为字符串

[英]As far as I know showinputdialog returns an output of type string but it gives me an error that it cannot convert the object to a String

 String val = JOptionPane.showInputDialog(null,
            "Search item to Edit ",
            "Warning",
            JOptionPane.QUESTION_MESSAGE,
            null,
            new String[]{"Item name", "Item no.","Price"},
            "");

Error:错误:

incompatible types: Object cannot be converted to String不兼容的类型:Object 无法转换为字符串

With that much parameters, the method showInputDialog doesn't return a String but an Object as stated in the javadoc有了这么多参数, showInputDialog方法不返回字符串,而是返回 Object,如javadoc中所述

As suggested in the Swing tutorial , you simply have to cast the result to String.正如Swing 教程中所建议的,您只需将结果转换为字符串。

String val = (String) JOptionPane.showInputDialog(null,
        "Search item to Edit ",
        "Warning",
        JOptionPane.QUESTION_MESSAGE,
        null,
        new String[]{"Item name", "Item no.","Price"},
        "");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想将一个对象类型 Stack 和一个字符串传递给同一个类中的另一个方法,但它给了我一个错误。 - I want to pass an object type Stack and a String to another Method in the same Class, but it gives me an error. 类型不匹配:无法从元素类型对象转换为字符串错误 - Type mismatch: cannot convert from element type Object to String error 将字符串转换为json对象会给出错误 - Convert string to json object gives an error 无法从元素类型对象转换为字符串 - cannot convert from element type object to string 我有这个错误:“无法将 java.lang.String 类型的对象转换为 com.flashpub.flash.imageSection 类型”,我不知道为什么 - I have this error : "Can't convert object of type java.lang.String to type com.flashpub.flash.imageSection" and i don't know why 我想将输出流转换为String对象 - I want to convert an output stream into String object 错误:类型不兼容:字符串无法转换为char opp = JOptionPane.showInputDialog(“ Enter Method”); - error: incompatible types: String cannot be converted to char opp = JOptionPane.showInputDialog(“Enter Method”); 错误:类型不匹配:无法从 Map 转换<object,list<object> &gt; 至 Map <string,list<> &gt; </string,list<></object,list<object> - ERROR: Type mismatch: cannot convert from Map<Object,List<Object>> to Map<String,List<>> 我怎么知道 Object 是否是 String 类型的对象? - How can I know if Object is String type object? 类型不匹配:无法从元素类型对象转换为字符串 - Type mismatch: cannot convert from element type Object to String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM