When a user writes in console characters, to confirm fact that his input is ready to be passed to application he presses enter key. But console doesn't pass only provided characters, it also adds to input stream (System.in) OS dependent line separator character(s) after it. Some OS use \r
or \n
(those are single characters, \x
is just notation to represent them) others like Windows use \r\n
(two characters) sequence as line separator.
Now those additional characters are also read by System.in.read()
and since they are not equal to S
System.out.println("wrong.guess again to finish the program");
is executed additional time.
To avoid such problems instead of working with raw data via System.in.read()
consider using classes meant to make our life easier like java.util.Scanner
Scanner sc = new Scanner(System.in);
System.out.println("Guess a capital letter Character");
String response = sc.nextLine();
while(!response.equals("S")){
System.out.print("incorrect data, please try again: ");
response = sc.nextLine();
}