2012-10-09 206 views
0

可能重複:
Scanner issue when using nextLine after nextIntfor或while循環跳過輸入

我創建需要同時讀取一個String並從我的服務器整數的客戶端程序。取決於它接收到的整數,它向GUI添加了一些標籤。到目前爲止,我的程序讀取整數,但跳過字符串。下面的輸出是我的程序的輸出,當我嘗試寫整數程序:

  • Server寫入:1
  • Server寫入: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; 
} 

希望有人能夠幫助我解決這個:)

預先感謝您。

+1

你在哪裏增加Ø? (也許我是盲目的?)如果時間== 0,你的while循環會跳過一個字符串。 – belwood

+0

可能這是你正在尋找或者它可以幫助你。 http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextint – Abubakkar

回答

0

我發現我的問題的解決方案,結果很簡單!

首先讓我解釋一下我的感受。

當我的程序運行while循環時,它基本上跳過了應該從服務器接收輸入的那一行。我發現它那樣做的原因是input.nextLine();是空的,這使得當你讀取input.nextLine();

將此掃描儀推進到當前行並返回跳過的輸入。此方法返回當前行的其餘部分,排除末尾的任何行分隔符。該位置設置爲下一行的開頭。 由於此方法繼續在輸入中搜索以查找行分隔符,因此如果沒有行分隔符存在,它可能會緩存搜索要跳過的行的所有輸入。 返回: 這是跳過

,因爲我試圖讓行是一個空行,將跳過,並設置名稱爲「」行了。

這裏是我的計劃全部完成代碼,並將其當前工作:

while循環:

  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++; 
      } 

的client.reciveString();

public String reciveString(){ 

    String x = input.next(); 
    return x; 
} 

的的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 if (j == 3){ 
     chatPerson3.setVisible(true); 
     lbl_userName3.setVisible(true); 
     lbl_userName3.setText(reciveChat);} 

感謝您對您所有的迴應,我一定會投你了:)

1

我沒有看到循環代碼中的任何地方你正在增加o或更改值times。所以或者循環被完全跳過(即:times = 0)或者代碼中的其他地方正在修改循環變量(o)或者循環條件(times) - 這兩種情況都是非常糟糕的編碼。

您的循環變量/增量規則在讀取循環時應該非常清晰,並且在不需要讀取可能會在循環迭代過程中修改值的其他方法/等情況下輕鬆辨別啓動/停止條件是什麼。

我的猜測是times = 0,否則你將處於無限循環。

+0

好吧,我的複製粘貼在while循環結束時失敗了,它沒有o ++; –

+0

好吧,這基本上是發生了什麼: 第一個程序開始,然後它要求我把時間設置爲1(例如),然後再次問我另一個數字(這是客戶端的ID)我設置爲1系統然後打印1,j1,名稱。 如果我嘗試設置一個字符串,我得到一個錯誤,因爲發佈的東西不是一個整數(它指的是reciveCommando),這是一個整數。 –

+0

我不關注。 「因爲發佈的東西不是int(它指的是reciveCommando)」。發佈了什麼內容? –