使用parseint将String转换为int(convert String to int with with parseint)

编程入门 行业动态 更新时间:2024-10-17 13:39:07
使用parseint将String转换为int(convert String to int with with parseint) java

我尝试将int String转换为int

int port = Integer.parseInt(tokens[2]);

tokens[2]包含一个String "12777"但是我收到了这个错误

Exception in thread "Thread-2019" java.lang.NumberFormatException: For input string: "12777" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:458) at java.lang.Integer.parseInt(Integer.java:499) at inet.ChildServer.hopen(ChildServer.java:88) at inet.ChildServer.run(ChildServer.java:51)

编辑:对不起,eclipse中的字符是不可见的,我不明白这是什么。 我有一个这种形式的字符串

command1|Destination-IP|DestinationPort

而我只是把它弄乱了

String[] tokens = sentence.split( "[|]" );

I try to convert int String to int

int port = Integer.parseInt(tokens[2]);

tokens[2] contain a String "12777" But I got this error

Exception in thread "Thread-2019" java.lang.NumberFormatException: For input string: "12777" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:458) at java.lang.Integer.parseInt(Integer.java:499) at inet.ChildServer.hopen(ChildServer.java:88) at inet.ChildServer.run(ChildServer.java:51)

Edit: Sorry the characters are invisible in eclipse, I don't Understand what is this. I have a String in this form

command1|Destination-IP|DestinationPort

and I just splot it

String[] tokens = sentence.split( "[|]" );

最满意答案

你可以在传递给Integer.parseInt(tokens [2])之前修剪字符串; 它可能包含空格。

can you trim the string before pass to Integer.parseInt(tokens[2]); it may contain blank spaces.

更多推荐

本文发布于:2023-04-28 02:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   String   parseint   int   convert

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!