2014-10-12 83 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到一个空行,所以你需要先清除缓冲区。