Java Tutorial – The Basics Part 2


As promised, in my last java basics tutorial, a few words on variables and scanners. Before I show you some code and how to use these objects it may be handy if I gave you a short intro on variables.

A variable is, as the word implies, a value which has not be defined up front. This value will be defined by actions within your program. Like for example: the result of a webservice, the outcome of a calculation, a value entered by a user, … .
There are many types of variables, strings (har har har..), integers, floats, doubles, characters, etc, … .
Depending on what you need and what you are doing, you will use a different type of variable.
We’ll also use scanners in java. Scanners are objects which allows your program to read user input. Basically you will capture what the user types.

Alright.. Now you know this we are all set. Let’s get started! Open up eclipse and make a new main class (if you aren’t quite following, read my previous java basics post).

Once this is done you should get a class with some default code in it just like last time. Let’s now declare a variable which will contain your name.
First off, you will need to declare a variable. Which means that you will have to make clear to the program there is a variable with a specific name.
Since it is a series of characters (because I assume your name is not A , E or R.. Come to think of it.. MR. X would be a problem, anyhow…), we will be needing a string (get used to using strings, because you’ll be needing them a lot).
We can declare a variable as followed: String Name = “Sharp”;
Just like before you need to type this within the {} of the main class.

Like this you have made a variable with a static value. In my case Sharp.
That’s nice, now let’s print out our name by typing following code below the declaration of the variable: System.out.println(Name);

Run the program by clicking the run button and notice that your name will be printed in the output box below the editor. Good job! Your first variable declaration. But it would be nicer if we could input our name on the fly don’t you think.
In order to do that we will need to define a scanner (and this will be the first confusing part of the java tutorials).µ

Here is how you declare a scanner:  Scanner read = new Scanner(System.in); Also remove your name from the variable. And change the system.out.println() to system.out.println(Name). This will print the value of your variable.
You may notice that eclipse underlines Scanner with a red line telling you: “You idiot! This is wrong!”.
And eclipse is right.. Well not about you being an idiot, but about the fact that this is wrong.

At this point in time your program does not know what a scanner is. You will need to import this class. Huh?! Yep, sorry that’s how it works. There are some predefined packages and classes you’ll need to import. Don’t worry if you don’t know if a class is available or not. Google is your friend.
To solve this issue we will need to import the class java.util.Scanner. Type following line AT THE TOP of your code. not between any class {} just on top of the page: import java.util.Scanner;

Notice the red lines in your code dissapear. Alright, so far so good. Now all we need to do is to put the value of what the scanner reads in the variable name. We do this with following code: 
Name = read.next();
(this says variable name equals the next value of the scanner READ).
The program is now ready to run. Hit the build button and notice nothing happens… Or does it? Ofcourse it does! You need to type your name in the output box below (which is also an input box). When you hit enter your name will show:

Your first variable scanner program.. Good job! ;-)

I realize this is a bit confusing when your starting to program. But you’ll get the hang of it soon. And again, google is your friend :), there are tons of code examples and tutorials out there (i’ll try to write a few myself).

Until next time!

Advertisements

2 thoughts on “Java Tutorial – The Basics Part 2

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: