1

I have one stupid question: how could I replace Scanner with BufferedReader in this method:

public Coordinates getShot() {
    Scanner scanner = new Scanner(System.in);
    int x,y;
    do {
        System.out.print("Enter Х: ");
        x = Integer.parseInt(scanner.nextLine());
        System.out.print("Enter Y: ");
        y = Integer.parseInt(scanner.nextLine());
        System.out.printf("\nYou entered: [%d,%d]\n", x, y);
        if(!Coordinates.checkCoordinates(x, y))
            System.out.println("Error");
    } while(!Coordinates.checkCoordinates(x ,y));
    Coordinates shot = new Coordinates(x, y);
    return shot;
}
HadJower5
  • 35
  • 8
  • 1
    What useful methods did you find while searching the javadoc of the `BufferedReader` class? How do these compare with `nextLine` and other features of `Scanner`? – Savior Apr 11 '16 at 14:35

1 Answers1

0

See Scanner vs. BufferedReader

Scanner is used for parsing tokens from the contents of the stream while BufferedReader just reads the stream and does not do any special parsing.

In fact you can pass a BufferedReader to a scanner as the source of characters to parse.

It would be possible, but would have no interest to use a BufferedReader here:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//....
int x,y;
System.out.print("Enter Х: ");
x = Integer.parseInt(reader.readLine());
System.out.print("Enter Y: ");
y = Integer.parseInt(reader.readLine());
System.out.printf("\nYou entered: [%d,%d]\n", x, y);
christophetd
  • 3,834
  • 20
  • 33