Wednesday, October 10, 2012

Java for beginners Part 2



Hello Me!


Welcome to your second Java for beginners tutorial, in this tutorial you will learn the simple hello Me program after you've learned Hello World!.

Things you will need:

  1. Java you can download it if it isn't already installed from Java official site free Java.
  2. NetBeans you can download it for free from NetBeans official site free NetBeans.
  3. Read Hello World! if you didn't read it yet From here.
  4. 10 minutes of your time.
NOTE: Its advised to download and install Java before installing NetBeans. 

Lets start:

Now that you ready to make your second program after installing Java and NetBeans lets open NetBeans, now you will have to create a new project there is lots of project's types you can choose from but we will start with the simplest and very basic one of them the Java application.


File>>New Project
you will get a window similar to this (it may look different depending on the version of your NetBeans and other installed programs you have or have not)
New Project
Project Type
Choose Java for the Categories then Java Application for the Projects then Next>.

Project Name
Project Name

For the name type HelloMe or whatever you like your program name to be, But in here I will be using HelloMe as the project name so its a good practice to do the same so that names wont make you confused.


Now that you created the project its time to start writing some codes, This is how your code will look like when you open HelloMe.java file


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hellome;

/**
 *
 * @author YourName
 */
public class HelloMe {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }
}

Our program is very simple all we want to do is to make our program ask for your name and Then welcomes you by saying Hello Yourname! and to make the program read anything from the Output Window we create an object from Scanner Class we will also use variables in this program.


NOTE: Variables are containers that hold our data temporary it has many types as we may need to store text or numbers or anything else
text = String       ex:"This is sentence"
number = int      ex: 1
point number = float       ex: 1.5 


NOTE: Scanner Class is A simple text scanner which can parse primitive types and strings using regular expressions.


Now lets make our program ask for your name and run it, In the Main type System.out.Println("What is your name?");
Now we need to create an object of Scanner Class and name it input or whatever you want.


Scanner name= new Scanner(System.in);


Then we need to get your name from the Output window so we will declare a variable called name of type string and put your name there first here is how to declare a variable of type string

String name;

its as easy as that but we need  to tell our program to put your name in this variable because now we reserved a string space in our memory and never used it and this is how to tell your program to read your name from the Output window and put it in the variable name.


String name = input.nextLine();


now all we need to do is to display the welcome message which is simple as we learned before how to display any text on the Output window but there is a little thing we will add there because we want the program to say Hello Your name so this is done like this


System.out.println("Hello " + name + "!");

What the plus sign means is add the text in this variable from type string to that text in that position

NOTE: Always remember to  put space after hello because if you didnt it will be HelloYourname without spaces it doesnt matter how many spaces you put after the quotation mark it will be ignored. 


Now that is how your final code will look like.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hellome;

import java.util.Scanner;
/**
 *
 * @author YourName
 */
public class HelloMe {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.print("What is your name?");
        Scanner name= new Scanner(System.in);
        String name = input.nextLine();
        System.out.println("Hello " + name + "!");
    }
}

Now lets Run our program and see what we did press F6 to run your program.

Output Result
The Result


Congratulations now you have done your second Java Program you are ready to learn more



NOTE: Click Download below to download sample project. More Download Links

Download From FileFactory!

Hope you enjoyed and benefited from this tutorial please feel free to comment or ask about anything and more tutorials will be added.
Thank you for reading and enjoy your day. 

2 comments:

  1. Great post and right to the point. I am not sure if this is in fact the best place to ask but do you guys have any idea where to get some professional writers? Thank you

    ReplyDelete
  2. Thank you so much, actually I don't know where to get one.

    ReplyDelete