2014-10-12 72 views
0

當我運行這部分程序時,它無法識別使用'sc.nextLine();'的兩個輸入語句,但它接受'sc.next();'的條目。有沒有人有什麼建議?語句'sc.nextLine();'的邏輯錯誤

String information() 
{ 
    int l;char at;String nam,id,number;long contact; 
    System.out.println("\nPlease enter your name"); 
    nam=sc.nextLine(); 
    do 
    { 
     System.out.println("\nPlease enter your contact number"); 
     contact=sc.nextLong(); 
     number=Long.toString(contact); 
     l=number.length(); 
     if(l<5||l>8&&l!=10) 
      error(); 
    } 
    while(l<5||l>8&&l!=10); 
    System.out.println("\nPlease enter your e-mail id"); 
    id=sc.nextLine(); 
    int len=id.length(); 
    for(int k=0;k<len;k++) 
    { 
     at=id.charAt(k); 
     if(Character.isWhitespace(at)) 
     { 
      error(); 
      break; 
     } 
    } 
    return nam; 
} 
+0

給出的錯誤是什麼? – Nabin 2014-10-12 10:48:12

+0

這是一個邏輯錯誤。因此,當我運行它時,它會顯示'請輸入您的姓名',留下一行並說'請輸入您的聯繫電話號碼'。因此,我不能爲前面的陳述輸入內容。 – 2014-10-12 11:01:21

回答

2

嘗試sc.nextLine

nam = sc.nextLine(); 

上面這需要nextLine到一個空行,所以你需要先清除緩衝區。