Java Tutorial Lesson 13: Keyboard input continued

From Erlands Wiki

Jump to: navigation, search

Now, lets make the keyboard handler a little better. The “Game” class has been modified so it looks as follows:

import java.io.IOException;
 
public class Game {
    public static void main(String[] args) {
        KeyboardHandler input = new KeyboardHandler();
 
        System.out.println("Lesson 13");
        System.out.println();
        try {
 
            // Get next command
            System.out.print("What would you like to do: ");
            int command = input.getCommand();
 
            // Go into loop unless quitting
            while(command != Command.QUIT) {
 
                // Check which command the user requests
                if(command == Command.WEST) {
                    System.out.println("Walking west");
                }else if(command == Command.EAST) {
                    System.out.println("Walking east");
                }else if(command == Command.NORTH) {
                    System.out.println("Walking north");
                }else if(command == Command.SOUTH) {
                    System.out.println("Walking south");
                }else {
                    System.out.println("What do you mean ?");
                }
 
                // Get next command
                System.out.print("What would you like to do: ");
                command = input.getCommand();
            }
            System.out.println("Exiting game");
 
        } catch (IOException e) {
            System.out.println("A serious error has occurred, exiting game");
        }
    }
}

There are a few changes from the previous lesson, the keboard handler now returns an integer and we have also defined constants in a separate Command class that looks as follows:

public class Command {
    public static final int UNKNOWN = 0;
    public static final int QUIT = 1;
    public static final int WEST = 2;
    public static final int EAST = 3;
    public static final int NORTH = 4;
    public static final int SOUTH = 5;
}

Your work this time is to update the “KeyboardHandler” class so it reads a command from the user as before but returns one of the integer constants defined in the “Command” class. The keyboard handler should also be able to identify commands if only the first letters has been entered. For example if the user enters “e” or “eas” these shall be interpreted the same way as if the user would have entered “east”.

As usual the application shall be executed as:

java Game

Here is a sample output of how it could look like when you run it:

Lesson 13
 
What would you like to do: we
Walking west
What would you like to do: east
Walking east
What would you like to do: 
What do you mean ?
What would you like to do: n
Walking north
What would you like to do: easter
What do you mean ?
What would you like to do: south
Walking south
What would you like to do: quit
Exiting game

Some optional things to try:

  1. Update the KeyboardHandler class so it will identify the commands even if they are entered with capital letters, for example “EAST”


Personal tools