簡體   English   中英

通過PrintWriter從服務器向客戶端發送整數時,無法進行解析

[英]When sending integer through PrintWriter from server to client, no parsing is possible

我正在編程一個小的服務器客戶端程序,該程序將一個正在寫文件的客戶端的文本發送到具有相同文件名的其他客戶端,並出現以下錯誤 在此處輸入圖片說明

但是我只是發送一個整數而沒有其他字符...

這是代碼:

服務器

String[] splitter = scanText.split("\n");
String length = splitter.length + "";

//sending scanText to clients
for (PrintWriter pw2 : userMap.get(filename) ) {
     if(!pw2.equals(pw))
     {
        pw2.println(length + "\n" + scanText); 
     }
}

客戶

類“ UpdateInBackground”是客戶端類中的類

class UpdateInBackground extends Thread {

    @Override
    public void run() {
        int lines; //to know how much lines are send from the server
        String scanText;
        while (!this.isInterrupted()) {
            scanText = "";
            lines = Integer.parseInt(sc.nextLine()); //here I get the error
            while (lines-- > 0) {
                scanText += sc.nextLine() + "\n";
            }
            output.setText(scanText);
        }

    }
}

@asparagus,請在sc.nextLine()行中定義sc,考慮到這是來自Scanner類的對象,我需要知道輸入。 這個問題必須能夠用變量的定義和輸入內容來解釋。

在類UpdateInBackground中,lines = Integer.parseInt(sc.nextLine()); //這里nextLine()用於任何String,請參考文檔

NumberFormatException的原因:您正在將值轉換為int,而不知道輸入內容是什么。

嘗試使用異常處理,以了解可能會出現哪些類型的錯誤,以避免程序被刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM