[英]Cannot convert String to Integer in Android Studio / Java
我有一個通過藍牙接收數字(精確到毫秒)的功能。
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
有任何想法嗎?
您的data
是一個字符串。 當您稍后嘗試形成Integer時,此操作將失敗。 您需要使用以下代碼: myInt = new Integer(Integer.parseInt(data));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.