Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 返回函数字符串中的整数_Java - Fatal编程技术网

Java 返回函数字符串中的整数

Java 返回函数字符串中的整数,java,Java,如何在函数字符串中返回整数 这是我的密码 public class Utils { public static String type(String s){ if(UraniumProtectMain.materialname){ return s; } else { return Integer.parseInt(s); //<--- It's } } } 公共类Utils{

如何在函数字符串中返回整数

这是我的密码

public class Utils {
    public static String type(String s){
        if(UraniumProtectMain.materialname){
            return s;
        } else {
            return Integer.parseInt(s); //<--- It's
        }
    }
}
公共类Utils{
公共静态字符串类型(字符串s){
if(铀保护主材料名称){
返回s;
}否则{

return Integer.parseInt(s);//返回对象或以字符串形式返回整数。

由于要同时返回整数和字符串,可以使用返回类型作为对象类,因为对象是所有类的超类,并且可以在对象变量中保存任何类型的值

public static Object type(String s){    
    // code here
}
现在,您可以在instanceOf方法的帮助下按类型获取返回值的类型

if(Utils.type() instaceOf String)
{
    // code here
} else if(Utils.type() instanceOf Integer) {
    // code here
}
正如所建议的,您可以将change方法返回类型返回给object

  • String.valueOf(number)(我的首选项)
  • 整数.toString(数字)

  • 要以字符串形式返回integer并在方法调用点执行类型检查,您的代码要完成什么?目前还不清楚。Java中的方法既不能返回string也不能返回int-您应该只使用一个。您最好解释为什么他必须这样做。他想同时返回integer和string这可以通过在Object类的帮助下,正如你所知,Object变量可以保存任何类型的值。他可以在instanceOf methodgreat的帮助下轻松获得类型。我写道,因为你的答案被标记为低质量的答案,所以在这里,你总是需要解释你的答案,就像你在给我的评论中所做的那样,但你必须在回答本身,以避免被标记为低质量的回答。