我試圖用插座
執行此代碼時我得到NumberFormatException的數字格式異常輸入字符串到整數
public class ThreadSocket extends Thread {
Socket socket;
int k;
ThreadSocket(Socket socket) {
this.socket = socket;
}
public void run() {
try {
String message = null;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((message = bufferedReader.readLine()) != null) {
System.out.println("Incomming message client : " + message);
k += Integer.parseInt(message);
System.out.println("la somme est :" + k);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
此異常收到消息:
Exception in thread "Thread-0" java.lang.NumberFormatException: For input string: "3 : 2"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at ThreadSocket.run(ThreadSocket.java:33)
所以接收的消息顯示爲'3:2'。我無法從你的問題中知道這是否是有意的,但你正在嘗試將tp convert轉換爲一個整數。你不能這樣做。您只能將字符串符合整數的語法,如'4','23463'或'-2034284621'。 –
您可能想告訴我們所需的行爲,它會更容易幫助您。 –
Incomming message client:user:+30 線程「Thread-0」中的異常java.lang.NumberFormatException:對於輸入字符串:「user:+30」 \t at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65 ) \t在java.lang.Integer.parseInt(Integer.java:580) \t在java.lang.Integer.parseInt(Integer.java:615) \t在ThreadSocket.run(ThreadSocket.java:33) – anonimosz