我有一个通过蓝牙接收数字(准确地说是毫秒)的函数。
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/