How to Write a Java Application Program That Prompts a User to Input One After the Other


Java applications commonly rely on user input in order to operate correctly. A program may prompt the user to input his name and password before loading. You can make a program that does this by prompting the user to input text using the console. Once the user name is entered, another prompt appears and asks for a password. You can prompt for many things, one after the other, using the same technique. Java makes this easy by providing built-in libraries for handling console input.

Things You'll Need

  • Java Development Kit and Netbeans Bundle (see Resource)
  • Start a new project in NetBeans by clicking on its icon, selecting "File/New Project", and choosing "Java Applicatoin." A new Java project is created and a source code file appears in the NetBeans text editor. The source code file has a main function and little else.

  • Import the "Console" class by writing this line at the top of the source code file:


  • Create a new Console object by writing the following line of code between the curly brackets of the main function:

    Console c = System.console();

  • Declare a couple of strings, one for a user's name, and one for the user's password. You can accomplish this by writing the following:

    String userName, password;

  • Prompt the user to enter his or her user name using the following line of code:

    userName = c.readLine("Enter your user name and press enter: ");

  • Repeat the last step for the user's password, like this:

    password = c.readLine("Enter your password and press enter: ");

  • Run the program by pressing F6. The program will prompt you to enter a name. After you enter your name, it will ask you for a password. You can add more prompts if you would like. All you have to do is repeat the previous step and add more strings to hold the data.

Promoted By Zergnet



You May Also Like

  • How to Create a Word Document in Java

    The Word format is a commonly used standard for desktop word-processing documents. The Java language provides developers with a language to create...

  • How to Write to the Director of Admissions

    Writing to any college or university's Director of Admissions is an intimidating task. Your final written product is the first impression the...

  • A Tutorial for AIX on Unix

    Log on as a user and change your password. On the operating system (OS) prompt, which is "%" for C shell, type...

  • How To Write Java Programs

    Java is an object-oriented programming language originally developed and released by Sun Microsystems in the mid-1990s. Java was developed from the start...

  • How to Get Keyboard Input in Java

    Capturing keyboard input in a Java console program is fairly straightforward and only requires a few lines of code. Console programs are...

  • How to Write a Method in Java

    Java is the most popular programming language, primarily because of its portability. The same Java program can be used on virtually any...

  • How to Create Registration Forms in Java

    Creating registration forms with Java by hand-coding them is a time-consuming and error-prone process. Fortunately, a number of free integrated development ...

  • How to File a Complaint With the Postmaster General

    The U.S. postmaster general is the chief executive of the U.S. Postal Service. The postmaster general's duties include oversight of consumer complaints...

Related Searches

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!