What will you build

You will see the different library to get input from user, from the console and from the arguments when running the application.

What You'll Learn

What will you need

To have some interaction with the user, Java offers some libraries for implementing code that will read the user input.

The first Class is the Scanner Class. It have been proposed in Java version 5.

To implement the Class Scanner, we need first to import it, as the Class is not loaded by default. The importation of a library is done before the declaration of the Class.

In a method getInputUser, we will declare a variable of type Scanner. We will instantiate it and initialise this variable. The constructor of the Scanner can take different type of variable as a parameter. As we want to be able to read the keyboard input, the parameter of the Scanner constructor will by a System.in.

We will output a message to the user to ask him/her to input a String. You should always provide clear instructions to the user before calling the Scanner method. This instruction is caller a prompt. In the next line, we are invoking the method nextLine() from the instance of the class Scanner. This method will return a String, by consequence, we are storing the return of the method in a String. We are finally returning this String.

It is always important to give clear instructions to the user when you want the user to interact with the program.

import java.util.Scanner;

public class ReadInputUser{

	public String getInputUser(){
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a String");
		String input = sc.nextLine();
		return input;
	}
	
}

We will test this method in a class.

public class InputUserTest{
	public static void main (String []args){
	ReadInputUser rd = new ReadInputUser();
	String readInput = rd.getInputUser();
	System.out.println("your message was "+readInput);
}

You can look at the documentation to find the different methods that this Class offers. Some methods will be very useful to directly return an integer, double instead of a String.

Documentation for the Scanner library

The class Console is another Class that allows the application to receive the input from a user.

import java.io.Console;

public String consoleInputUser(){
		Console c = System.console(); 
		String userInput = c.readLine("Enter a message, please ");
		return userInput;
	}

documentation for the Console Class and the readLine method

Another way to have an input from the user is for the user to enter an argument when they are launching the application. In java, this argument will always be an array of String. This is the array of String defined in the signature of the main method.

To be able to read the value of the argument, a loops need to be implemented and the different arguments given by the user could be extracted.

To be sure that the user did enter some arguments and that the array of String contains a value, we implement first a test. This test will evaluate the length of the array, and if the length of the array is bigger than 0, the body of the if contains a loop that will loop through the value of the array and display it. If the length of the array is smaller than 1, the body of the else contains a System.out.println and will display a message to the user to ask them to enter an argument.

It is important to always give a very clear message to the user.

public class ReadArgument{

public static void main(String []args){
		if (args.length > 0){
			for (int i = 0; i<args.length; i++){
			System.out.println(args[i]);
			}
		} else {
		System.out.println("please enter an argument");
		}	
	}
}

To test this application, compile it, and when you want to run it, write the java ReadArgument [argument].

java ReadArgument Hello

If everything went well, you should see Hello in your console. You can try with several words following each other with spaces, the JVM will take the different arguments, store them in the array of String and the loops that is implemented above will display them line by line.

You can find the code for this example in https://git.cardiff.ac.uk/ASE_GROUP_2020/code_for_codelabs.git