简体   繁体   English

错误转换可选<String>从 TextInputDialog 到整数

[英]Error converting Optional<String> to Integer from TextInputDialog

In this example I have tempSocket1 and tempSocket2 but I really just want one of them.在这个例子中,我有 tempSocket1 和 tempSocket2,但我真的只想要其中之一。 I just included both to show I tried both methods, but I keep getting an error, "the method valueOf(String) in the type Integer is not applicable for the arguments (Optional)."我只是将两者都包括在内,以表明我尝试了两种方法,但我一直收到错误消息,“Integer 类型中的方法 valueOf(String) 不适用于参数(可选)。” I thought both of these methods were the ones used for converting a string data type to integer, but I'm not sure how the Optional part changes the whole system.我认为这两种方法都是用于将字符串数据类型转换为整数的方法,但我不确定 Optional 部分如何改变整个系统。

private void showTextInputDialog() {
        TextInputDialog changePort = new TextInputDialog("Settings");
        changePort.setHeaderText("Change Port");
        changePort.setContentText("Please enter port number to be used for establishing connection...");

        Optional<String> result = changePort.showAndWait();
        result.ifPresent(e -> {
            Integer tempSocket1 = Integer.valueOf(result);
            Integer tempSocket2 = Integer.parseInt(result);
            }
        );
}

To convert an Optional to an Integer, it is necessary to invoke the get() method before the conversion.要将 Optional 转换为 Integer,必须在转换前调用 get() 方法。

Optional<String> cadena = Optional.of("333");
Integer num = Integer.valueOf(cadena.get());

You see, Integer.valueOf and Integer.parseInt methods need an argument of type String , but you are passing an Optional<String> .你看, Integer.valueOfInteger.parseInt方法需要一个String类型的参数,但你传递的是一个Optional<String> So that's why the error occurred.所以这就是错误发生的原因。 Optional string and string are not the same.可选字符串和字符串不一样。

Just think about this, if Optional<String> were the same as String , would ArrayList<String> be the same as String ?试想想,如果Optional<String>分别为相同String ,将ArrayList<String>是相同的String Would LinkedList<String> be the same as String ? LinkedList<String>String吗? What about HashMap<String, Integer> ? HashMap<String, Integer>怎么样? Would it be both a String and an Integer ?它既是StringInteger吗?

The chaos that treating generic types the same as their generic type arguments would bring is destructive!将泛型类型与泛型类型参数一样对待会带来的混乱是破坏性的! Imagine calling charAt on an optional string!想象一下在可选字符串上调用charAt Without the implementation, no one knows what will happen...没有实施,没有人知道会发生什么......

So yeah, never think that generic types are the same types as the generic type parameters.所以是的,永远不要认为泛型类型与泛型类型参数是相同的类型。

You're trying to pass an Optional<String> instead of a normal String .您正在尝试传递Optional<String>而不是普通的String You need to fetch the string first with .get() before converting your result to an integer.在将结果转换为整数之前,您需要先使用.get()获取字符串。 Or use result.ifPresent(e ...) that will automatically unwrap the optional value and convert it to an Integer.或者使用 result.ifPresent(e ...) 将自动解包可选值并将其转换为整数。

Optional<String> result = changePort.showAndWait();
result.ifPresent(e -> {
    Integer tempSocket1 = Integer.valueOf(e);
    Integer tempSocket2 = Integer.parseInt(e);
    }
);

Just to extend other answers it may looks better using map method, and even more with lambda and method reference:只是为了扩展其他答案,使用map方法可能看起来更好,使用 lambda 和方法引用甚至更多:

Optional<String> result = changePort.showAndWait();
Integer tempSocket = result.map(Integer::valueOf).orElse(8080);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM