Console Input

Although when we are programming our robots, we will not be needing to accept input from the console, we will be reading input from other sources. In addition, for the purposes of these introductory exercises we will be needing to get console input.

We will start by creating a program that will ask you for your name and then print it back out.  To create this new program, you can either modify your HelloWorld program, or create a new one. We have already used System.out to output to the console, and we are now going to use System.in to read data.  Unfortunately, the class gives us access to the console data as a stream of bytes whereas we really want to read it as a text String. To accomplish this we are going to use the Scanner class which we will attach to the console input.

Consider the following code:

import java.util.Scanner;

public class HelloWorld 
{
	public static void main(String[] args) 
	{
		Scanner input = new Scanner(System.in);
		String	myName;
		
		System.out.println("Enter your name");
		myName = input.nextLine();
		System.out.println(String.format("Your name is %s", myName));
		
		input.close();
	}
}

On line 7, we create an instance of our Scanner class based on the console input.  Don’t be too concerned with how this code works as we will be addressing class objects and their creation in a later chapter.  For now, just take this as a ‘magic cookie’ enter that line into your program. Note that we added the line:

import java.util.Scanner;

This gives us access to the package in which the Scanner class is defined.

On line 8 we declare a variable of type String. While we have not talked about this class before, we have used it that we have used literal strings enclosed in quotes (the “Hello World” we had in our original program is an example of this).  This will be the variable where we will store the string we get from the console.

On line 10 we ask the user to enter their name, and on line 11 we use the input.nextLine() function to read the next line from the console, and then print that name back out on line 12.

Finally, we call the close() function of our Scanner class since we will no longer be requiring that input.

When we run our program, it should ask for your name in the Console window. You can then click down in the window and type your name, which should then result in the following output:

Enter your name
John
Your name is John

Beyond strings

Using the Scanner class we can read other types besides Strings.  For example, consider the following code:

	public static void main(String[] args) 
	{
		Scanner input = new Scanner(System.in);
		int age;
		
		System.out.println("Enter your age");
		age = input.nextInt();
		System.out.println(String.format("Your age is %d", age));
		
		input.close();
	}

Now we are asking for your age and will be expecting you to enter an integer, so we replace the call to nextLine() with a call to nextInt(), which will return the next integer typed at the console. Run this program and then click down in the Console window and enter your age and you should see the following:

Enter your age
99
Your age is 99

Using this Scanner class you can also read input in the form of all of the 8 primitive data types.

Next: Control Statements