Possible Duplicate:
Scanner issue when using nextLine after nextInt
Below is my code:
import java.util.Scanner;
public class Test{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
StringBuilder sb=new StringBuilder();
int count=0;
for(int i=0;i<n;i++)
{
count++;
sb.append(sc.nextLine());
sb.append("\n");
}
System.out.println(sb);
System.out.println(count);
}
}
I am not able to write the inputs for the last run of the loop, i.e., when i==n-1
,although the variable count
equals n
at the end. Where is the error?