1

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
9
  • There are 3 characters in a\r\n. You're using Windows? Commented Jul 4, 2018 at 6:54
  • Possible duplicate of stackoverflow.com/questions/12898455/… Commented Jul 4, 2018 at 6:55
  • it doesn't print that just tried but when I enter anything it prints twice means the iteration runs twice Commented Jul 4, 2018 at 6:55
  • @DanyalSandeelo and you're using a Mac? Commented Jul 4, 2018 at 6:55
  • nope, i am on win Commented Jul 4, 2018 at 6:57

3 Answers 3

2

The InputStream.read method block until the end of input which is on a Windows OS the control characters CR LF (\r\n).

This explains why you get 3 characters as a result.

See for yourself :

public static void main(String[] args) throws java.io.IOException {

    int i;

    System.out.println("Type S to stop.");

    char c = (char) 0;
    for (i = 0; c != 'S'; i++) {
        c = (char) System.in.read();
        System.out.println("Pass # " + i);
        System.out.println("char intValue : " + (int) c);
    }
}

Suggested read : Java: How to get input from System.console()

Sign up to request clarification or add additional context in comments.

1 Comment

This code works. I like the way you introduced the (int)c to make me follow the trail. Now I checked CR LF on the ascii table and I see they are printed each time hence the three outputs. Thanks
1

System.in will use BufferedInputSteam to read the input from console bit by bit (including the line break, etc). In Mac system, I get 2 bits whenever i gave a single digit input.

Use Scanner and read all the bytes and convert as String.

Comments

0

I suggest you to avoid reading input in that way, use Scanner instead. You can reach your goal with this code:

    int i = 0;
    Scanner scan = new Scanner(System.in);
    System.out.println("Type S to stop.");
    while( !scan.next().equals("S")) {
        i++;
        System.out.println("Pass # "+i);
    }

2 Comments

You still need to consume the linebreaks - this doesn't solve anything.
I tried it and it works. I had to import Scanner. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.