Skip to content

User Inputs

When we trigger our test automation execution on dedicated servers, we will initiate test process by providing test related input and config details in Terminal. It's vital for us to be able to process user inputs.

In this code note, we will cover following topics:

  • What is User Inputs
  • Scanner Class
  • String Concatenation
  • Summary



› What is User Inputs

In terminal based application, end user can interact with the application through text inputs. The term User Inputs refers to these text inputs provided by the end user as part of using the terminal application.

As an example, the terminal based echo program lets you provide text inputs for printing out echo message.

Providing input to echo application

From above sample:

  • echo:   it is the program name
  • hello alphaleaf :   it is the user inputs provided by the end uesr

Note that everything you type in the terminal window is treated as text data (String).



› Scanner Class

In Java, user input for terminal based Java application is supported by methods grouped in theScanner class. We can use these methods in this class by creating a code copy ( called Class Object ) of this class.

Scanner scan;                 // variable for storing Scanner class object
scan = new Scanner(System.in);// assigning Scanner class object to variable

This class contains useful methods that enable use to parse the user inputs from the terminal window.

You can process any user inputs by following three easy steps.

Step 1:   Create a Scanner object:

Scanner scan = new Scanner(System.in);

Step 2:   Prompt & Process the inputs:

System.out.print("Please enter a name: ");
String inputName = scan.nextLine();

Step 3:   Destroy the Scanner object:

scan.close();

Some useful methods includes:

Method Name Method usage purpose
scan.next() Used for parsing one complete word input
scan.nextLine() Used for parsing single line of text inputs
scan.nextInt() Used for parsing single integer number input
scan.nextDouble() Used for parsing single decimal number input
scan.nextBoolean() Used for parsing single logic data input



› String Concatenation

String data uses concatenation operator for combining two different string data into one larger string data. This operator is especially useful when you are forming output string.

  • operator symbol:   +
1
2
3
4
5
String first = "John";
String last  = "Miller";

String full = first + last;   // <-- concatenation
System.out.println(full);     // JohnMiller

Note that string concatenation operator converts all the non-string data type operand into String data type as long as one side of the operator is string data.

Right Side is Number
1
2
3
4
5
6
7
8
String title = "User's birth year is ";
int    year  = 1990;

//              string   integer
//             -------   ------
String output = title  +  year;

System.out.println(output);   // User's birth year is 1990
Left Side is Number
1
2
3
4
5
6
7
8
double  rate = 89.03;
String title = "% is the max rate";

//             double   string
//             ------   ------
String output = rate  +  title;

System.out.println(output);   // 89.03% is the max rate