Java Tutorial: User Interaction

In the first tutorial I discussed how to set up a very simple java program, if you have not checked it out or forgot how you can find it here.  If you do remember how to set it up then lets get started!

Last program wasn’t to interesting I will admit but it was a start.  In this tutorial we will discuss how to set up your program to get user input!  Say we want to ask a user for their age, instead of statically typing in our age using the print statement.  We need to ask the user for their age, and then store that name into a variable and use the information stored in that variable within our print statement!  Here is an example of the code to do this.

import java.util.Scanner;

class UserAge

{

    public static void main(String[] args)

    {

         Scanner scan = new Scanner(System.in);

         int userAge;

         System.out.print("Please enter your age: ");

         userAge = scan.nextInt();

         System.out.println("Your age is " + userAge);

    }

}

Okay now it might look a little intimidating at this point, but have no fear all will be revealed!  Lets look at the first line in the source code a little bit.

import java.util.Scanner;

This is an import statement and will be used whenever you want to bring in a class from an outside source.  In this case the scanner is in the util package of java.  Because it is a class you will need to make sure you make Scanner uppercase otherwise you will encounter an error.  This class is what you will use any time you want to get input from a user.  The next couple lines should look familiar from the previous tutorial as the class name and the main function definition.  Something I forgot to mention is that I will go over the importance of String[] args at a future date, but for now just put it there just because.  The next couple of lines are new though.

Scanner scan = new Scanner(System.in);

int userAge;

The first is the creation of a new object and the second is the creation of a new variable.  As you can see they are a little different but don’t worry to much about the details of an object.  That is a more advanced topic that will be covered later.  You will notice some similarities in the declarations though.  Both start with the type followed by the name.  In the case of creating the scanner the type is Scanner and the name is scan.  The other side of the equals sign is the creation of the new Scanner object and passing it the nessesary parameter that will be needed to read the user input.  Like I said don’t worry to much about the details just yet, and just type it in the way you see it here.

The way the variable is created is very similar except with the int data type we don’t need to create an object.  In this case the variable has the name userAge.  There are many primitive data types for storing information but I will list some of the ones that will be used the most within this series of tutorials.

Data Types:

  • int – stores integers (ex:1, 33, -322)
  • float – stores floating point numbers (ex: 1.3, 22.3455)
  • String – stores a string of letters
  • boolean – stores true or false

While this list does not hold all of the data types, the ones I left out are very similar to ones I listed that can be found using Google if you find these data types to be too small.  Now that we have covered the different data types and how to declare variables we can go to the next section of code.

System.out.print("Please enter your age: ");

userAge = scan.nextInt();

System.out.println("Your age is  " + userAge);

The first part of this block of code should be familiar from before.  The only thing I want to point out is that I used print here because I wanted the user to enter their age right after the colon so it looked a little cleaner.  The next line is new though, and it is assigning the next integer entered by the user to the variable userAge that we created earlier.  nextInt() is a method scan that gets the next integer, and the equals sign assigns userAge that variable.  The of course we have another print statement, but this time there is a new element. The + userAge after the string is called string concatenation.  Essentially we are tacking on whatever is in the variable userAge to the end of our string literal.  If you forget to close the quotes before adding the variable it will literally print the variable userAge rather than the integer stored in it.  This is just a nice way to show the user that you got their input!

Next Time

That is the end of this java tutorial! Next time we will discuss a very simple yet powerful tool.  The if-else statement.  If you haven’t already and you are interested in learning out to program, please like and follow so you can get my new tutorials as they come out.  But don’t quit reading yet there is still a practice problem for this tutorial.

Practice Problem

I think a good way to get this idea down pat is to make your own version of Mad Libs.  This will allow you to use string concatenation as well as use the scanner class.  Try to use as many data types as you can, and don’t forget that when you are getting a name or any string of text use the String data type.  Do note that it is a capital S in this case, while the other primitives are lowercase.  Good luck and have fun!

Advertisements

Tags: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: