gpt4 book ai didi

java - 无法在 Android Studio/Java 中将字符串转换为整数

转载 作者:行者123 更新时间:2023-12-01 11:30:46 25 4
gpt4 key购买 nike

我有一个通过蓝牙接收数字(准确地说是毫秒)的函数。

byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "UTF-8");
readBufferPosition = 0;

started = "";
int myInt;
if (data != "") {
try{
myInt = new Integer(data);
long seconds = TimeUnit.MILLISECONDS.toSeconds(myInt);
}catch(NumberFormatException ex){
System.err.println("NumberFormatException "+ ex.getMessage());
}
...
} else {
...
}

上面的尝试抛出:

NumberFormatException Invalid int: "8250

我认为收到的数据不仅仅包含毫秒,但是当我这样做时我如何才能找到它

myLabel.setText(data);

它向我显示:8250

有什么想法吗?

最佳答案

您的数据是一个字符串。当您稍后尝试形成一个整数时,就会失败。你需要使用这个:myInt = new Integer(Integer.parseInt(data));

关于java - 无法在 Android Studio/Java 中将字符串转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30414544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com