java NumberFormatException 错误 (parseInt)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5267241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:19:23  来源:igfitidea点击:

NumberFormatException error (parseInt)

javastringintegernumberformatexception

提问by Alex

Hopefully a very simple query, but it's left me scratching my head.

希望是一个非常简单的查询,但它让我摸不着头脑。

I have a string, which is just a single integer, and I'm trying to then get that integer out as an int. This on the face of it shouldn't be a problem.

我有一个字符串,它只是一个整数,然后我试图将该整数作为 int 输出。从表面上看,这应该不是问题。

// this is how I create the string (it's the playload from a UDP datagram packet, 
// thought I don't think the origins hugely important - it's juts a test run so the
// stringMessage is always 1 (created by a seperate client process)

  ...
  recvSoc.receive(pac);
  String stringMessage = new String(pac.getData());
  port = pac.getPort();
  System.out.println("RECEIVED: " + stringMessage + " on port:  " + port);
  processMessage(stringMessage);
  ...

// Then in processMessage

public void processMessage(String data) {
  int message;
  message = Integer.parseInt(data);
  ...

This always crashes with a NumberFormatException error. I cannot for the life of me figure out what's causing this, any ideas greatly appreciated. I haven't coded much in Java (recently) so might simply be forgetting something critical or what not.

这总是因 NumberFormatException 错误而崩溃。我一生都无法弄清楚是什么导致了这种情况,任何想法都非常感谢。我(最近)用 Java 编写的代码不多,所以可能只是忘记了一些重要的东西或不重要的东西。

Exception in thread "main" java.lang.NumberFormatException: For input string: "1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:514)
at udp.UDPServer.processMessage(UDPServer.java:85)
at udp.UDPServer.run(UDPServer.java:52)
at udp.UDPServer.main(UDPServer.java:156)

采纳答案by Joachim Sauer

Note that DatagramPackate.getData()returns the whole buffer!

请注意,DatagramPackate.getData()返回整个缓冲区

The data you received is only a part of it:

您收到的数据只是其中的一部分:

The data received or the data to be sent starts from the offsetin the buffer, and runs for lengthlong.

接收到的数据或要发送的数据从offset缓冲区中的开始,并运行length很长时间。

So to convert the data to a Stringyou should use this constructor:

所以要将数据转换为 aString你应该使用这个构造函数

String message = new String(pac.getData(), pac.getOffset(), pac.getLength(), "UTF-8");

Note that I specify the UTF-8 encoding here, as not specifying an encoding would result in the platform default encoding to be used, which is generally not what you want.

请注意,我在这里指定了 UTF-8 编码,因为不指定编码会导致使用平台默认编码,这通常不是您想要的。

回答by Bozho

If the string is really 1, the exception can't happen. So I would say the string is not actually 1.

如果字符串是真的1,则不会发生异常。所以我会说字符串实际上不是1.

do a data.toCharArray()and print each character's code (cast to int). It may turn out that there is a hidden character before the digit, for example. (edit: it appears iluxa mentioned this option in a comment while I was writing the answer)

执行 adata.toCharArray()并打印每个字符的代码(转换为int)。例如,结果可能是在数字之前有一个隐藏字符。(编辑:似乎 iluxa 在我写答案时在评论中提到了这个选项)

Try data = data.trim()before passing it to parseInt(..)

data = data.trim()在将其传递给之前尝试parseInt(..)