20 Questions: Interact with the User

12.01.2018 |

Episode #2 of the course An introduction to Java programming by Alex Theedom

 

Welcome to the second of ten lessons about the Java programming language. I hope you are still enthused from the first lesson and are raring to go.

Let’s jump straight in. Today, you are going to write code that asks the user for their name—that’s you—and prints a hello message to the console using the name entered.

 

Step 1: Preparation

• Go to the online IDE, codiva.io, sign in, and click the New Project button.

• Call today’s project, Lesson 2.

• Create a class file called, ChatBuddy. This is where your code will go.

• Once you have created the code file, Codiva will automatically create the HelloWorld code that you saw in the first lesson. It will do this every time. Just delete the line, System.out.println(“Hello Codiva”);, to make space for your code.

Now you are ready to start coding.

 

Step 2: Start Coding

You’re going to write five lines of code today.

• The first line of code you will write scans the keyboard, waiting for you to enter your name, and it look like this: Scanner scanner = new Scanner(System.in);. This code creates a new Scanner called scanner, and tell it to scan the System.in (another way to say keyboard) for key presses. Enter the code above EXACTLY as you see it, between the code, public static void main(String[] args) {, and the matching curly brace, }.

Java is case sensitive, so uppercase and lowercase matters. Your code should now look like this:

class ChatBuddy {

   public static void main(String[] args) {

     Scanner scanner = new Scanner(System.in);

   }
}

• The Scanner is from Java itself and is available to programmers to use in their programs. However, you need to explicitly state that you want to use it by including an “import statement” at the top of the class file. To do this, add the code: import java.util.Scanner; above the class ChatBuddy { line of code.

Now you need to write code that asks the user to enter their name. You do this with a System.out command, like the one you saw in the first lesson.

• On the next line, enter the following code: System.out.print(“Enter your name: “);. This code prints the message, “Enter your name:”, to the console.

• Next, you need to instruct the program to wait for the user to enter a name. You do this by using the scanner you created earlier. Enter the code: String name = scanner.next();. This code waits for the next time the user presses the ENTER key and stores whatever the user typed on the keyboard in the variable name.

What is a variable? A variable is a place where data, like the user’s name, can be stored for later use.

Now that you have the user’s name stored, you want to print it back to the console with a nice message. Again, we use our old friend, System.out, to print the message.

• Enter the code: System.out.println(“Hello ” + name);. Notice the way the name variable is “added” to the message in quotes. The + sign glues the two together to make one message that can be printed to the screen.

Your program is now complete.

 

Step 3: Check and Run Your Program

Here is the code in full so you can check that you entered it all correctly:

import java.util.Scanner;

class ChatBuddy {

   public static void main(String[] args) {

     Scanner scanner = new Scanner(System.in);
     System.out.print(“Enter your name: “);

       String name = scanner.next();

     System.out.println(“Hello “ + name);

   }
}

Now run your program by clicking the Run button in the top right-hand corner of the screen. If all went well, you will be asked to enter your name in the console at the bottom of the screen, and when you press enter, the Hello message is displayed.

Congratulations, you have successfully completed your second Java program!

In Lesson 3, you will learn how to write code that does basic math. So, see you then.

 

Recommended book

Java: Learn Java in One Day and Learn It Well by Jamie Chan

 

Share with friends