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”);

The code snippet given below displays the use of the command `Console.WriteLine’ -

static void Main(string[] args) { int num_var_1;

Console.WriteLine(“Please enter a number”); num_var_1=int.Parse(Console.ReadLine()); Console.WriteLine(“The number you have entered is {0}”, num_var_1); }

In the above code snippet, the first `Console.WriteLine’ command is used to display a simple message: “Please enter a number. “

The second `Console.WriteLine’ statement, makes use of a {0} with the message, where 0 acts as a placeholder. When the computer displays the output, it replaces the placeholder with the variable specified after the comma (,), which is num_var_1 in the example

If the user enters the number 56, the message that will be displayed is:

The number you have entered are 56.

Just as a single placeholder is used to input a single variable or value, multiple placeholders can be used to show multiple values or variables. The code snippet given below illustrates the use of this concept:

static void Main(string[] args) {

int num_var_1;

string string_variable_name;

Console.WriteLine(“Please enter name of your best friend”);

string_variable_name = Console.ReadLine();

Console.WriteLine(“Next, please enter age of your best friend”);

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

Console.WriteLine(“Your best friend {0} is {1} years old”, string_variable_name, num_var_1);

}

In the example given above, the last `Console.WriteLine’ statement uses two placeholders. When the program is run and the output is displayed, the first placeholder {0} will be replaced by the value stored in the variable ` string_variable_name ‘. Similarly, the second placeholder {1} will be replaced by the value stored in variable ` num_var_1′.

If we input John as name of best friend and 32 as his age, the final output of this program will be – “Your best friend John is 32 years old”.

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>