I am new to Java and learning it. I was trying out a code to actually see how the for-each loop works in Java. But I faced a problem regarding that. My code is simple :
From the Java API:
Reads the next byte of data from the input stream.
What happens is when you type a followed by a newline, there are actually three bytes to read (I'm guessing a followed by carriage return and newline). Hence, the input you provide is enough to keep read() running for three iterations.
I recommend you to use Scanner for input reading instead.