-3

The purpose of this loop is to stop as soon as a period key is typed, but it is not working and I did not understand why.

import java.io.IOException;


public class ControlFlowTest {

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

    do{
        ch = (char) System.in.read();
    }while(ch != '.');

}
LuA
  • 3
  • 2

1 Answers1

0

You will want to use Scanner as System.in.read() will block until a carriage return is entered. Try this:

import java.io.IOException;

public class ControlFlowTest {
    public static void main(String[] args) throws java.io.IOException {
        char ch ;

        Scanner scanner= new Scanner(System.in);
        do {
            ch = scanner.next().charAt(0);
        } while(ch != '.');
    }
}
gyleg5
  • 144
  • 1
  • 2
  • 11
  • This will do the same thing, only worse. With your solution entering "abc." and pressing enter will not exit the loop. – Oleg Aug 19 '17 at 00:21