How to Write a Java Program That Prompts for a User's First & Last Name & Reads Them Separately

There are many common programs that are used to showcase a programming language's syntax, such as the popular 'Hello World!' application. Another common program is one that prompts the user for some kind of input, usually in the form of his or her first and last name. This kind of program is more advanced since it deals with user input. If you are learning the Java language, you will want to learn how to handle user input.

Things You'll Need

  • Java Software Development Kit with NetBeans bundle (see Resources)
Show More

Instructions

    • 1

      Launch the NetBeans software by clicking on its icon. Choose "File/New Project" from the menu items at the top of the window, then select "Java Application" and hit enter. A new Java project is created and a source code file appears in the NetBeans editor. The source code file contains an empty main function.

    • 2

      Import the console class by typing "import java.io.Console;" at the top of the source code file.

    • 3

      Create a new console object by typing "Console c = System.console();" within the curly brackets of the main function.

    • 4

      Create a prompt for the user's first name by typing "String nameFirst = c.readLine("Enter your first name: ");" below the line written in Step 3.

    • 5

      Create a prompt for the user's last name by typing "String nameLast = c.readLine("Enter your last name: ");" below the line written in Step 4.

    • 6

      Run the program by pressing F6. The console prompts you for your first name. Type it in and press enter. The console prompts you for your last name. Type it in and press enter.

Related Searches:

References

Resources

Comments

You May Also Like

Related Ads

Featured