Let’s write the following (terrifying, and quite horrifying!) Java program and try to execute it.

package testingproject;
/**
 *
 * @author your name
 */
public class TestingProject {
  /**  
   *
   * @param args the command line arguments
   */
  public static void main(String[] args) throws java.io.IOException {
    System.out.println("Hello World");
  }
}

Notice: If you don’t know how to create a new Java project in NetBeans IDE, click here.

In window “TestingProject.java”, delete the commented line “// TODO code application logic here” and type only the first letter from the System statement by hitting the capital “S” key on your keyboard. Next, hit the CTRL+SPACE key combination. A popup window appears, as shown in Figure 1. This window contains all available Java variables, constants, methods, and other items that begin with the letter “S.”

5

Figure 1 The popup screen in the Source Editor window

You can highlight a selection by using the up and down arrow keys on your keyboard. Notice that each time you change a selection, a second window displays a small Help document about the item selected.

Type the second letter “y” from the System statement. Now the options have become fewer as shown here. 5a

Select the option System.out.println("|"); and hit “Enter ↵” key. The statement is automatically entered into your program as shown here!

5b

Complete the statement by writing “Hello World” inside double quotes (as shown in Figure 2). Your NetBeans IDE should look like this.

6

Figure 2 Entering a Java program in the “TestingProject.java” file

Now let’s try to execute the program! From the main menu, select “Run  Run File” or hit the SHIFT+F6 key combination. The Java program executes and the output is displayed in the “Output” window as shown in Figure 3.

7

Figure 3 Viewing the results of the executed program in the Output window

Congratulations! You have just written and executed your first Java program!

Now let’s write another Java program, one that prompts the user to enter his or her name. Type the following Java statements into the NetBeans IDE and hit SHIFT+F6 to execute the file.

package testingproject;

public class TestingProject {
  
  public static void main(String[] args) throws java.io.IOException {
    java.io.BufferedReader cin = new java.io.
            BufferedReader(new java.io.InputStreamReader(System.in));
    String name;

    System.out.print("Enter your name: ");
    name = cin.readLine();
    System.out.println("Hello " + name);
    System.out.println("How do you do?");
  }
}

Remember! You can execute a file by selecting “Run  Run File” from the main menu, or by hitting the SHIFT+F6 key combination.

Once you execute the program, the message “Enter your name” is displayed in the “Output” window. The program waits for you to enter your name, as shown in Figure 4.

8

Figure 4 Viewing a prompt in the Output window

To enter your name, however, you must place the cursor inside the “Output” window. Then you can type your name and hit the “Enter ↵” key. Once you do that, Java interpreter continues executing the rest of the statements. When execution finishes, the final output is as shown in Figure 5.

9

Figure 5 Responding to the prompt in the Output window

Leave a Reply

Your email address will not be published. Required fields are marked *