Basic Java – 17 || Runtime User Input using Console Class (Part-3).

There is also a third way to take user input via using Console class. This method helps a user to take inputs via Command Prompt.

Before proceeding further, I would request you to please read the first two parts of this tutorial.

Basic Java – 15 || Runtime User Input using Scanner Class (Part-1).

Basic Java – 16 || Runtime User Input using BufferedReader Class (Part-2).

Console Class
Console Class

Facts about Console Class

  1. It uses Console Class from the java.io package.
  2. readLine() method reads a string from the console.
  3. readPassword() method reads data from the user without echoing on the screen.
  4. It stores data in encoded form read by readPassword() method.
  5. It works only with the Command Prompt. If we run this program in IDE, we could encounter exception java.lang.NullPointerException.



Code Example

import java.io.Console;

/**
 * @author ashok.kumar
 *
 */
public class UserInputConsole {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		/* To run this code follow below steps:
		1. Copy the entire code and paste it new Word or notepad Document.
		2. Save the document with the Class name, example, "UserInputConsole.java"
		3. Open the command prompt.
		4. Locate folder which contains UserInputConsole.java file.
		5. type, javac UserInputConsole.java and press Enter.
		6. type, java UserInputConsole and press Enter.
		7. enter the values and observe the output.
		*/
		Console con=System.console();
		
		System.out.print("Enter user name: ");
		String uName=con.readLine();
		
		System.out.print("Enter Password: ");
		// Read user input without echoing the text on screen or command prompt.
		char[] uPwd=con.readPassword();
		
		System.out.println("User Name: "+uName);
		System.out.println("Password appears in encoded form: "+ uPwd);
	}
}

Output:

Output_1


As we have already discussed that above code will execute only on Command Prompt. Below are the steps to execute the same on command prompt:

  1. Copy the entire code and paste it new Word or notepad Document.
  2. Save the document with the Class name, example, “UserInputConsole.java
  3. Open the command prompt.
  4. Locate folder which contains UserInputConsole.java file.
  5. type, javac UserInputConsole.java and press Enter.
  6. type, java UserInputConsole and press Enter.
  7. Enter the values and observe the output.

We can see that in the above code example, we have used System.out.println() to print the on-screen message. Then we have used readLine() and readPassword() methods to take inputs.

There is another version of these methods as mentioned below:

  • readLine(String format, Object args);
  • readPassword(String format, Object args);
  • printf(String format, Object args);

In the below example, we could see that how on-screen message and user input get embedded and prints on the console.

import java.io.Console;

/**
 * @author ashok.kumar
 *
 */
public class UserInputConsole_Format {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Console con=System.console();
		
		// Using String format while taking inputs from Console.
		String uName=con.readLine("Enter userName: ");
		char[] uPwd=con.readPassword("Enter Password for user ID '%s': ",uName);
		String uMobile=con.readLine("Enter mobile for user ID '%s': ",uName);
		
		con.printf("UserName: %s, Mobile: %s",uName,uMobile);
	}
}

Output:

Passing arguments using main() method

We can also pass the arguments using the main() method of the class using the Command prompt. We have to just pass arguments along with the java syntax while running the script.

cmd> javac UserInput_CommandPrompt.java
cmd> java UserInput_CommandPrompt Java C C++ .Net

  • In the above syntax Java C C++ .Net are the 4 arguments passed for this class.
  • Arguments should be separated by whitespace
  • It can take n number of arguments.

Code Example

/**
 * @author ashok.kumar
 *
 */
public class UserInput_CommandPrompt {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		System.out.println("Total arguments passed by user: "+args.length);
		
		int i=1;
		for (String string : args) 
		{
			System.out.println();
			
			// We have to use printf() method as println() method takes only one argument.
			System.out.printf("arg[%d] : %s",i,string);
			i++;
		}
	}
}

Output:




 


Related Links:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:


 

1238total visits,4visits today

Leave a Reply

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