サーバーから文字列と整数の両方を読み取る必要があるクライアント プログラムを作成しています。受け取った整数に応じて、いくつかのラベルを GUI に追加します。これまでのところ、私のプログラムは整数を読み取りますが、文字列をスキップします。次の出力は、整数をプログラムに書き込もうとしたときのプログラムの出力です。
- サーバー書き込み: 1
- サーバー書き込み: 1
- システムプリント: 1
- システム印刷: j1
- システムプリント: 名前
問題は、文字列をスキップするため、文字列を書き込めないことです。この問題を回避するにはどうすればよいですか (for
ループも試したことに注意してください)
私のコードは次のとおりです。
int times = client.reciveCommando();
int o = 0;
System.out.println(times);
while (o != times) {
int j = client.reciveCommando();
System.out.println("j"+ j);
String name = client.reciveString();
System.out.println("Name " +name);
createUser(j, name);
o++;
}
createUser メソッド:
private void createUser(int j, String reciveChat) {
if (j == 1) {
chatPerson1.setVisible(true);
lbl_Chatperson1_userName.setVisible(true);
lbl_Chatperson1_userName.setText(reciveChat);
} else if (j == 2) {
lbl_chatPerson2.setVisible(true);
lbl_userName2.setVisible(true);
lbl_userName2.setText(reciveChat);
} else {
chatPerson3.setVisible(true);
lbl_userName3.setVisible(true);
lbl_userName3.setText(reciveChat);
}
}
client.reciveCommando メソッド:
public int reciveCommando() throws IOException{
Integer i = input.nextInt();
return i;
}
client.reciveString メソッド:
public String reciveString(){
String x = input.nextLine();
return x;
}
誰かがこれで私を助けてくれることを願っています:)
前もって感謝します。