Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

How Do You Get Keyboard Input in Java?  

Ok, so for now, I'm assuming you're using a keyboard, we'll talk about how to get keyboard input.  In Java, we need to use something called a Scanner object (we'll talk about what objects are very soon as well).  First, at the top of the program, let's be sure to include this line:  

import java.util.Scanner;  

Next, we need to set up and create the object, which looks something like this:  

Scanner inputReader = new Scanner(System.in);  

Scanner is the type of object we're creating, and we're giving it a name of inputReader in this example.  You can call it something different if you want, and in fact, in the code snippet, we've called it readInput.  Giving it a name allows us to call and use it later in the code.  Note that we need to create and name the object before we can use it, otherwise we'll get an error when the program runs.

The new Scanner part of the line actually creates the new Scanner object, and the System.in part in between the parentheses tells us that the Scanner will read input from System.in, which in this case, represents the user's keyboard.   

So taken all together, in plain English, we can say that this line:

Scanner inputReader = new Scanner(System.in);

is basically saying "We're creating a new Scanner object called inputReader, which will check for input from the player's keyboard."  

After we set up a Scanner object, we need to actually use it to read input from the user.  Let's say that we want to get a line of text that the player enters, such as a player name, for example.  We could do something like this:

String playerName = inputReader.nextLine();  

A String is a type of Java object that represents text.  Remember when we used System.out.println() to output text to the screen?  Everything in between the quotation marks was a String object.

Our example line above, in plain English, means "Create a String object called playerName, and use it to store the next line that the player types on the keyboard."  A line of keyboard input ends when the player presses the Enter key, and at that point the Scanner object will read that line.  

Again, since we named the String object, we can use it later in the program if we want to.  For example, we can print out a welcome message:

System.out.println("Welcome to the game, " + playerName);  

We're using System.out.println() again, but now we see a + sign.  This will print out the first part between the quotation marks, which is a String, and then it will add whatever we entered for playerName.  Everything between the quotation marks is called a "string literal," because the program will print it out exactly as it's shown.  playerName is a variable, that represents a String value that can be changed.  Remember in math how you had variables called x and y, where you didn't necessarily know the value at first, but you knew it had to be a number?  Variables in Java are the same basic principle.  So since both "Welcome to the game, " and playerName are both Strings, we can use the + sign to combine, or concatenate, them together.  If the player had typed in the name "Terra" for playerName, then the output would be "Welcome to the game, Terra".  

We will be talking more about variables soon.  But for now, this is one basic way to work with input and output in Java.   

Now let's look back at the code snippet I posted at https://codepad.co/snippet/AR6xKxde.  I've put in a few lines of code that wait for the player to press the Enter key.  In the code snippet, on line 9, you can see that I've set up my Scanner object, which I've named readInput.  We then have a String called keyPress, which waits for the player to type something, then press the Enter key.  There's a bit of a trick here on line 12.  In some games, you might want the player to just press Enter, and not worry about any other input.  On line 12, the player didn't enter any text, but just pressed Enter, then the keyPress String should be empty, or "".  Notice that there is NO SPACE or anything in between the quotation marks, it's just an empty String.  Whitespace counts as part of a String, so if there's a space there, let's say that the player hit the space bar before pressing Enter, it's not an empty String.  Line 12 checks to see if the player entered an empty String, and if the String is empty, the program prints a nice intro.  Else, on line 15, the program will let the player know about following instructions :)