I am trying to make the loop continue to execute here until the user types the letter S at the keyboard. It seems to be giving three outputs instead of one for each iteration. What am I doing wrong here;
// Loop until an S is typed
public class ForTest {
public static void main(String[] args)
throws java.io.IOException {
int i;
System.out.println("Type S to stop.");
for(i = 0; (char) System.in.read() != 'S'; i++)
// System.out.println("print");
System.out.println("Pass # " + i);
// System.out.println("print");
}
}
Output coming up is if I press 'a':
Type S to stop.
a
Pass # 0
Pass # 1
Pass # 2