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

JAVA – Console Output Commands


Just as the Console Input command, accepts the user defined input, the Console. Output command displays the output on the monitor. In the Java programming language, you can write programs that write text lines to the console, which is a DOS based command window.

Though it’s not as popular as a Graphical User Interface or a GUI, the Console.Output command is used sometimes by programmers. Also, the Console.Output command doesn’t work properly with applets, WebStart applications and normal GUI programs. A sample code that shows the dialog or the console output program for simple input/output command is shown below:

public class example_Console_Output

{

public static void main(String[] args) {

System.out.println(“Hello, This is the example of Console Output command. “);

}

}

The System.out.println command is used to display an entire line of output on the console. The message passed as an argument to the System.out.println command is printed on the console. The println is the short form of print line command and originates from the Pascal programming language.

Continue reading JAVA – Console Output Commands