Introduction to Java #10 - User Input

User input is one of the coolest functionalities of any programming language. You get to write some fun code and then enter in your own data through the console and see what your code does to it. Obviously, you write the code, but it’s still interesting to see the code work differently depending on what you input. Let’s get into it.

The first thing you need to do to set up user input is import a library. To do this, write import java.util.*; in the very first line of the program. Then, you need to create a Scanner object. Do this by writing Scanner sc = new Scanner(System.in). This is what you should have right now in your text editor:

So what does this mean? Let’s start with the import statement. Frequently in Java and other programming languages, you need to import libraries that carry more functionality than what the basic programming language has. I imported the utility library here. Then, I created a Scanner object, which I named sc (you could replace sc with anything you want, it’s just the name of the object it doesn’t matter). This object comes from the utility library. We won’t get into the specifics of objects right now, but basically it is a thing that lets us do more. I know that’s vague, but objects, classes, and methods, are not the focus of this tutorial. Just know for now that the object will help us with user input.

Now that we have the object, we can begin scanning for user input. Let’s say I wanted the user to enter their age so that I could print out how far away they are from 100. Here is what that would look like:

There’s a lot to break down here so let’s go line by line. We already know what line 6 does, it creates a scanner object. Line 8 then prints out “Enter your age: “ in the console. This only purpose of this statement is to let the user know it is time to enter their age into the console. Then, on line 10, the magic happens. The user’s age is stored in the variable “user_age". Note that it’s helpful to have descriptive variable names. Now how does the data go from the console to the variable? Well, we use the scanner object, sc, and one of the object’s method, nextInt(), to scan the data. Note that nextInt() only looks for integers, so you couldn’t enter a string. Once the data is stored, the difference is calculated in line 12. Lastly, in line 14, there is a simple print statement telling the user how many years they are away from 100. Here is what the console would look like if you ran this and entered 10 as the age:

Great! Now we know how to scan integers. There are so many different applications of this. Let’s look at another!

In this program, I set a secret number equal to 5. I then have the user try to guess the number! If they are correct, I print “Correct!” If the are wrong, I print “Incorrect!” As you can see, I implemented if statements as well. If statements are frequently used in conjunction with user input, for they let you navigate through and make decisions with data. Let’s look at what the console would look like for an incorrect and a correct guess.

Here is one more example with strings instead of integers:

This simple programs asks for the name of the user, and then it greets them! As you can see, instead of using nextInt() with the Scanner object, you have to use nextLine() for strings. That’s the only difference, everything else is the same! Here is what the output would look like if you are curious:

Great, now we know how to do user input! Tune in next time for a lesson on arrays!

Previous
Previous

Introduction to Java #11 - Arrays

Next
Next

Introduction to Java #9 - Boolean Operators