Java, getting user data: class Scanner

Until now we define variables when we were programming.

But what if we wanted to get information from the user?
For example, ask for age or to create a calculator? Would depend on what was typed.

We will use the Scanner class to receive these data.

Importing classes and packages

There are thousands of features in Java. These classes were grouped into packages.
And packages for the same functionality are called API (Application Programming Interface). For example, we have a section on the Java 2D API, to make 2D drawings.
In other words, are a series of packages to draw.

However, all these packages are simply not ready to be used, because they are many.
Even you can create (and will) your packages, you can download, reuse, share, sell etc.
If all these were ready for use, it would take A LOT to run a Java program.

What is the solution then?
Let's say we want to use Java which features. For this we use the function 'import':
import package_that_you_want;

For example, to use print, printf and println, is not necessary say anything to Java.
They are so common methods that can be used in any application automatically.
Such methods are part of a package called "java.lang. '

Getting user data: new Scanner (System.ini)

To receive user data, we have to use the Scanner class, which is part of the package 'java.util'.
Let's say that we use the Java class in our application
To this, add this line at the beginning of the program:
import java.util.Scanner;

Well, we have the class. Let us declare our object of type Scanner.
Let's call it 'input'. It statement is made as follows:
Scanner input = new Scanner (System.ini);

Ready, object 'input' is used to read entries from the system.

Remember that there is a strong typing behind the scenes. In other words, Java is dealing with bytes, memory blocks and other things more complicated.
So for Java, there is much difference between integers, floats, doubles, and other types. So we need to be very clear about that.

Thus, our entry is well-typed. Let's start by integers.
To receive an integer from the user, with our object 'input', we use the following syntax:
integer = input.nextInt();

Explanations behind, let's see the thing working. This is a very simple example that asks the user's age, he expects to enter (and hit enter) and displays this message on the screen:

import java.util.Scanner;
public class Entry {

    public static void main (String [] args) {
        Scanner input = new Scanner(;
        int age;
        System.out.println("Enter your age:");
        age = input.nextInt();
        System.out.printf("Your age is" + age + "\n");

But this 'how old is it? ... 19 ... you're 19', is so tele-tubbie thing.

You're Java programmer, aka Paladin Arts Computing.
The following program uses a function that gets the current year (user's computer informaton) and calculates the year you were born, something that is more useful to repeat what you just typed.

For this, we use the class 'Calendar', which has methods for working with days, hours, days of the week, minutes, seconds, years, etc.
To learn more about, visit the documentation:

To use the 'Calendar', you must import:
import java.util.Calendar;

We will use the method get (Calendar.YEAR), which returns an integer with the year, and we store this integer in a variable 'year'.

So, our program looks like this:

import java.util.Scanner;
import java.util.Calendar;

public class Entry {

    public static void main (String[] args) {
        Scanner input = new Scanner(;
        int age;
        int current_year;
        int birth_year

        // Question and stores the age
        System.out.println("Enter your age:");
        age = input.nextInt();
        // Create an object of type Calendar, the 'calend' and storing the current year
        Calendar calend = Calendar.getInstance ();
        current_year = calend.get(Calendar.YEAR);
        birth_year = current_year - age;
        System.out.printf("You were born in" + birth_year + "\n");

The year of birth is calculated and stored by subtraction operation:
birth_year = curret_year - age;

You will learn other mathematical operations soon.
This is worth a caveat.

I did so because it is a basic tutorial and as a matter of organization, but the variable 'ano_nascimento' would not be necessary.
We could have used '(curret_year - age)' in direct printf like this:
System.out.printf ("You were born in" + (curret_year - age) + "\n");

But it must be enclosed in parentheses.
So you no longer need the variable 'birth_year'.

Incidentally, also would not need the variable 'curret_year' could have done so directly:
System.out.printf ("You were born in" + (calend.get (Calendar.YEAR) - age) + "\ n");

You know what? Forget the variable 'age', do not use variables at all:

import java.util.Scanner;
import java.util.Calendar;

public class Entry {

    public static void main (String[] args) {
        Scanner input = new Scanner(;
        System.out.println("Enter your age:");
        Calendar calend = Calendar.getInstance ();
        System.out.printf("You were born in" + (calend.get (Calendar.YEAR) - input.nextInt() ) + "\n");

Notice how we were taking the variables and the code is getting smaller?
Every time we took off some variables the program gets smaller and takes up less space.

But lost in readability. Note that it is now harder to understand what we did.
With 'age', 'current_year' and 'birth_year' is much more organized.

There is no method or better way of doing things. Programming is so personal thing .
You will need to find a compromise between efficiency (program fast and which occupies little space), but that is easily understood by others (and by you in the future).
For writing very compact and complicated, it may be that you do not understand what made the future.

Receiving other types of data, float: nextFloat();

In the previous example, we use the integer type.
If you are astute will notice the 'int' in 'nextInt()'.

Yes, to float, will be 'nextFloat':

import java.util.Scanner;

public class Entry {

    public static void main (String[] args) {
        Scanner input = new Scanner(;
        float price;
        System.out.println("How much costs something in a $1.99 store?");
         price = input.nextFloat();
        System.out.println("What? " + preco + "?");

Create an application that asks the birth year and say the current age of the user.
Use the Calendar class and method get(Calendar.YEAR) of this class.

PS: There may be some problems in calculating his age
1. On account of the month you make birthday.
This problem will be solved leaving the most complete, wondering what month and day of birthday and using conditional tests, we learn later.

2. If the user does not enter a number or enter a negative or other nonsense.
At that moment you will feel comfortable calling him individual, possessed and stuff.
But get used.

There is something called error handling and exceptions, very used to just try to imagine the possible errors and exceptions that may occur.
In case exceptions, is the beautiful name that gives absurdities that users can enter.

For example, you may have seen in entries:
Enter your birth date in the form mm/dd/yyyy:

Then the user will enter and 5/janeiro/89 and do not know what went wrong.
Try now enter something other than a number in their programs.

Using the Scanner class to receive data that the user types.

No comments: