ファイルを書き込んでいるクライアントからのテキストを同じファイル名の他のクライアントに送信する少しのserver-client-programmをプログラミングしています、および サーバからクライアントへPrintWriterで整数を送ると、解析はできません
次のエラーが発生しました。しかし、私はちょうど...
を整数や他の文字を送信しています。ここのコードです:
サーバー
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);
}
}
}
ありがとうございました! しかし、なぜ出力が明らかに表示されない文字が行数(1)の前にあるのですか? – asparagus
あなたの入力(sc.nextLine())、または一連の入力を知ることができますので、解決策をお手伝いします。 – Rubal
これを追加するには、不要な文字をチェックし、コンソールに次のように出力します:System.out.println(sc.nextLine()。length)、これをIntegerに解析します。 – Rubal