Custom Search

Monday, May 16, 2011

Our first program!

In this post we will be building our first Real Java program! The program will ask the user for his name, and his favorite food. Let’s begin by opening Eclipse. Click File, new, Java project.  In the popup leave everything as defaults and then name your project Test and hit finish. Right click on the SRC folder and click new class. Name the class Main
Put this into your main class so it looks like this.
import java.util.Scanner;
public class Main {
public static void main(String[] args){
     
}
}

We will start at the top and work our way down. 
import java.util.Scanner;
here we are importing the Scanner class from the java.util package.  You might be asking, but we never downloaded a java.util package.  Well actually in comes with the JDK.  A Scanner is something that can get input from the keyboard.
public class Main {



}
Declaring our class public(available to be viewed by other classes) declaring it a class, and declaring its name.  The curly braces are used to define the classes body(where you put stuff to make stuff happen).
 public static void main(String[] args){
     
}
Our main method.
METHOD: What the class uses to make something happen.  A  procedure for accomplishing something.
  Declaring the method public.
  Declaring it static(will cover this later) Declaring it void(does not return anything)
Declaring that it’s our main method.
  We will cover this in greater detail in later posts, but for now just put it.
Scanner name = new Scanner(System.in);
      Scanner food = new Scanner(System.in);
String username;
      String favoritefood;
Declaring two new Scanner variables name and food. Then declaring two strings(strings are text) and naming them username and favorite food.
System.in: telling the program that the scanner needs to take something in(the users name, and his favorite food).
import java.util.Scanner;
public class Main {
public static void main(String[] args){
      Scanner name = new Scanner(System.in);
      Scanner food = new Scanner(System.in);
      String username;
      String favoritefood;
      System.out.println("What is your name");
      username = name.next();
      System.out.println(username + " What is your favorite food?");
      favoritefood=food.next();
      System.out.println("So " + username + " your favorite food is "+ favoritefood );
}
}

Our finished code. Don’t worry we will go over it!  We are printing “What is your name” and then telling the system that username is equal to name(don’t worry about the .next() ).  The Scanner name needs to take something in so that’s where you come in.  The system will wait until you type something in to take the space of the Scanner name.  Then we print the String username(the users name which is equal to the text you put in). Put a space before the W or it will run together like this:  TimWhat is your favorite food? The system waits again until you type something in for the users favorite food.  Finally it prints out “So ” + username “ What is your favorite food is” + favoritefood.
Type this into eclipse and click the run button.  The box at the bottom serves as the command prompt box in eclipse. This is where you will type in your name and favorite food when prompted.
Well, that finishes up this post.  If you guys have any ideas or questions feel free to leave a comment!

No comments:

Post a Comment