JAVA – Console Input

Console input refers to keyboard. Most of the times we assign value s to the variables while declaring them. However, there are times when the program depends on the user provided inputs also. For example:

String your_name_example;

your_name_example  = Console.ReadLine();

In this example, first statement declares a string variable ` your_name_example ‘. Next statement, the Console.ReadLine command will read the value from console that is value which is entered by the user and store it in the variable ` your_name_example ‘.

This command works great if you assigning and entering the values into string variables. However, the input that the computer receives from using the command Console.ReadLine(); does not work with data types such as integers or doubles. This command will accept inputs in the text format only. If you try to input other types of variables using Console.ReadLine() command, an error message is generated.

Instead you can use the parse command as shown below:

int num_var_1;

num_var_1 = int.Parse(Console.ReadLine());

In the above example, the int.Parse command will convert the text input from the string output of  Console.ReadLine() into an integer variable num_var_1.

Similarly, the command double.parse is used to enter a double data type user defined value.

Not only this, the command `Console.WriteLine’ can be used to display messages and values stored in variables. For example,

Console.WriteLine(“Hello. This is an example of display message on screen”);

Continue reading JAVA – Console Input