Java 4 tutorial for beginners. In and out from console using Scanner.

Uploaded by edu4java on 05.06.2011

Hello I'm Eli for edu4java
and today we are going to see
data entry and data printing through the java console
We had already seen the printing of data through the console. Data entry is a bit more complicated
our tutorials are going to be more fun when we learn to do it
First thing of all -new-java project-
Let's create a project
called tutorials

We are going to create
a new package
Packages are to organize java classes.
As I' m going to use this package to keep several tutorials
I'm going to do it to organize myself
Java to help you organize classes -classes go into a file, usually-
uses packages. These packages
keep these files
In this case we are going to use
a package called


Here we can see another characteristic;
packages are normally defined with a lower case
and with words separated with a dot
usually you use a domain; here I'm using a domain;
to create an unique identifier of the package

Later on we will take another look to packages
Here we have a package
Now we create a class
As we can see there is something new
this declaration "package"
In the same file of the java class
it tells us in which package it is
When I created this class I forgot to create the main method
as you remember it is the method which defines the initiation of the execution.
A class can only be run if it has a method "main and static"

If you write main and you press ctrl+space, Eclipse offers me to create
the method main automatically
"public static main"...
Eclipse generates everything for me
This is all at the same height. To understand it better we usually format the code
If we format the code we can see what is inside what. This method which is inside
this class should be in another line
If we press
Eclipse is going to format the code
Until now
we had seen that "syso + ctrl+ space"
allowed us

to print on the console

Run As, java application....
As you can see "edu" is written on the console
This would be the printing of the data. We are sending data from our program to the console
What we need to make this more interesting is to feed the program with data
java has a class called Scanner which allows us to create an Object
which reads from the console and delivers the data to the program using a method called "next"
let's see how it works

We have to declare a type Scanner variable

Here we have something new
new Scanner;

Here we have a creation of an Object.
This definition of a variable was what we saw in the past tutorials, but this variable has something special
The type of this variable; "Scanner"...
-see that the variable are written by convention with lower case and the classes by convention with upper case-
This variable "scanner" belongs to the class "Scanner" with upper case

With this new
what we do is create an instance
of the object "Scanner"
and we are going to save it in "scanner"
To create this instance of the object "Scanner"
we are asking the class "Scanner" to give us a new instance

a new object of "Scanner"
Classes are like molds from which we can
create a new object with this mold. "Scanner" is the class and when we write new Scanner what we obtain
is going to be an object which belongs to
the class "Scanner"

This is not a tutorial about Objects
so I recommend you go to the part of objects in "edu4java" if you feel lost
but anyway I am talking a little bit about objects so that you start to feel familiar with them
When we create "Scanner" we pass him an argument
We are telling him what to scanner
We want him to scanner ""
"" is going to be the console. It will scanner this
As before we said print "System.out"
and it was printed here. In this case it is "". It is "in" because from here we are going to get data.
You don't really have to understand it. Just so that you get familiar with the concept. Will see it again in the part of objects

I can ask
the "Scanner"
to give me the next one
It has a method which
gives me the next. The next, what?
The next data entry. We are going to call it "entrada"
What I'm trying to do here is to assign a variable "entrada" what it's read through the scanner
Here I have an error
Here we have a variable which is not defined yet. Here we have another variable which is already defined;"scanner".I ask for help
"Create local variable"entrada"" and automatically it helps me out and writes the variable type
And here we have the creation of the variable
and the assignment

I have the object "Scanner". I asked this object "Scanner" to give me the next data entry
I am going to keep it in this variable
next I'm going to print the variable
Let's see what happens
We run it and apparently nothing happens...
What happened is it ran this line, it created the Scanner object
and here was executed
This method is waiting for me to write something on the console. When I write
"edu" on the console

automatically it goes to the next instruction
and when I press "return" this ends and it inserts in the "entrada" variable "edu"
and "edu" is going to be printed
with "System.out"
"entrada". The first one was written by me and the second "edu" was written by this "entrada"
Maybe you can see it more clearly if

I write more comments
Let's write before it asks for "entrada"; let's write "syso" ctrl+space
let's write

"escanear:" (scanner)

and after
the scanner finishes let's write
"syso" ctrl+space
"mostrar escaneado:"
(show scanner data)
"mostrar lo escaneado:"
Let's run this
and here
again scanner was created
it got here and it printed "escanear:" it got to this method and this method stopped the execution.It is waiting for us to
write something on the console
it continues to the next line

"mostrar lo escaneado"
And afterwards we write the "entrada" content
This is the variable. "entrada" was filled with what the object "scanner" read from the console
and here it is printed

With this
which is a bit complicated because I'm already talking about objects,
the creation of objects, with parameters...
I'm talking about methods...
The object "scanner" has a method "next" which returns
a String
It is a bit complicated but I wanted to do this so that
we could work with Scanner so that tutorials can be more interesting
With this our program can entry data to our program and it is going to be more interesting
This is all for this tutorial
See you in the next tutorial!!