Input and Output Data in java With Examples

Java output Data:

Input and Output Data:- Let’s start with the output of data that we already have from Hello World! is known: Printing with the method that applies to the System.out object, write strings in the window where your Java program. On Eclipse, this is the Console, otherwise, a command prompt or terminal window. println also accepts as parameters numbers that it automatically enters into strings. It is also allowed to use a string with additional expression by +. Then all expressions are first converted to strings; these strings are matched and then output.

An alternative to printing is print. The essential difference is that print does not end the output with an end-of-line character. This allows multiple print statements to be printed in a row. You can force a newline if you use the \n.

Of course, there are a lot of ways to get numbers and other Format data, such as to make only the first two decimal places.



Print():

This method in Java is utilized to show a text on the console. This text is passed as the parameter to this method through String. This method prints the text on the console and the cursor stays toward the finish of the text at the console. The following printing happens from simply here.

Syntax of print() method:

System.out.print(values);

Example of print() method in java:

Output:

println():

This method in Java is also used like print() to show a text on the console. It prints the text on the console and the cursor moves to the beginning of the next line at the console. The next printing happens from the next line.

Syntax of println() in java:

System.out.println(values);


Example of println() method in java:

Output:

Example: how to print literal and variable using output data method System.out.println():

Output:

Example: how to concatenate the two variables within the input method system.out.println():

Output:


Java input Data

Just as System.out references an object to standard output system.in to the standard input. From there, a Java program can Process input. The program must follow the method next to the Class java.utils.Scanner. Before the first input, such a scanner object can be created with new. next reads a String whose input must be terminated with (enter button). So that the users of the program know that they should enter something, it is useful to print a prompt:

At the latest at the end of the program, you should select the Scanner object, that references the scan variable. In our program that is not absolutely necessary, but it is recommended that all objects, that access files or other resources, explicitly as soon as possible close with the close method.If you forget it and in Eclipse, the development environment displays a warning on. Now the problem remains that we don’t expect strings can. We must first convert the input strings to floating point numbers. The method Double.parseDouble helps us in this.

double x = double. parseDouble (s);

However, the method assumes that the string is really is a floating point number with a point before the decimal point. If the users of our program enter 1,4 or abc, the program execution breaks with an unpleasant error message (java.lang.NumberFormatException).



Example: how to get data from the user and print on the screen in java using input and output methods:

Output:

Example: how to enter string, double, and float values in java input and output methods:

Output:


example: how to calculate the area and scope of a rectangle in java using input and output methods:

The following listing shows the finished Java program. In its execution, you must first enter the length and width. Where: is a Java-recognizable number, the program then returns the circumference and the area of the rectangle.

OutPut:


Related Article:

Variables in Java Context: Validity level, Object Variables, Wrapper Classes, Class variables

Java Literal: Boolean, whole Numbers, Floating point numbers

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: