Getting input

So far we've just been able to calculate numbers and show them to the user. Now we want to get the user involved in the program by prompting him for some input. We do this by calling a function which returns a String variable. We then use this String to dynamically create output to dialog with the user.

Strings are variables too

We already talked about using int variables. There's another very common variable type that we will use extensively in our game. It is called a String. It is basically a "string" of any number of characters. It can be a paragraph, a sentence, a word, or just a single character. A String is specified by enclosing the desired characters in double-quotes. Here's an example:
String greeting = "Hello, welcome to Stakeout!";
In some ways this is very similar to int's. We specify the type of the variable (String), then the name of the variable (greeting), then we assign a value ("Hello, welcome to Stakeout!") to it by using the equals sign. We can perform operations on Strings similar to int's. One of the most common operations is to add two or more Strings together.
String greetingPrefix = "Hello";
String greetingSuffix = ", welcome to Stakeout!";
String greeting = greetingPrefix + greetingSuffix;
We can also do this without explicitly declaring each of these variables by doing:
String greeting = "Hello" + ", welcome to Stakeout!";
The usefullness of being able to add Strings together will become apparent later in this lesson.

Calling functions

We've already called a function in the previous lessons. It was called "System.out.println". To call a function, you specify the name of the function followed by a comma-separated list of parameters which are enclosed in parenthesis.
System.out.println ("Hello");
In this case we are calling the System.out.println function with one String parameter. System.out.println only takes one parameter, so there's no need for commas. The parameters give input to the function and change its behaviour. A function will only accept a certain number of parameters and these parameters have to be of a certain type and they have to be in the right order. The number, type, and order of the parameters that a function will accept can be determined by looking at the file where it is defined. System.out.println is an "internal" function. We don't have access to the files where internal functions are defined, but fortunately there is documentation available that will tell us the information we need. The documentation for System.out.println is available here. It is a function of System's out member which happens to be a PrintStream object. If that last sentence didn't make any sense to you, don't worry. It doesn't matter that much right now. It is important to become familiar with the documentation because you will refer to it a lot. The master page for the Java documentation is located here. Make sure you bookmark it. If you call a function with the wrong number or type of parameters the compiler will complain, but it is not always clear what the problem is simply from looking at the compiler's error message.

As you can see from the documentation, there are several different versions of println which takes different parameter types. This allows us to use the same function name to perform a similar function on different types, but ultimately we are calling different functions. This is called function overloading.

Also from the documentation we see that println returns void, which means it doesn't return anything. Some functions will return values and we can assign these values to variables. Math.abs is an example of a function that returns a value. It computes the absolute value of an integer.
int absoluteValue = Math.abs (-3);
If we fail to assign the return value from the function to a variable, it is lost. We won't be able to use it later in our program.

Getting Input

One of the primary uses of String variables is to store input from the user. Getting input from the user is a little tricky. To hide the gory details I have created a function that you can call to perform the operation. Download the CmdInput CLASS file and save it in your working directory (the directory where your other code resides). There is a function in this file called "GetInput". It takes 0 parameters. It prompts the user for input and then returns a String value representing one line of input. It can be used to create an introduction sequence to gather information from the user that we can use in the game. One piece of information we'll need from the user is what his name is. Here's a complete program that you should try to compile.
public class Stakeout {

    public static void main (String args[]) throws java.io.IOException {
        System.out.println ("Please enter your name and press [Enter]:");
        String input = CmdInput.GetInput ();
        System.out.println ("Hello " + input + ", welcome to Stakeout!");
        System.out.println ("Game Over.");
    }
}
Now we can see the usefulness of being able to add Strings together. We can use this ability to dynamically create new Strings that includes user input.

Follow-up exercise

Create your own game startup sequence. Prompt the user for:
  • the number of players
  • the names of the players
  • whatever else you want


Rate this lesson: *****