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.
1 2 3 4 5 |
System.out.println (" Hello World!"); int i=3; System.out.println (i); double x =1.3323; System.out.println ("x is " + x); |
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:
1 2 3 4 5 6 7 8 |
class demo { public static void main(String[] args) { System.out.print("Hello "); System.out.print("World "); System.out.print("programming digest"); } } |
Output:
1 |
Hello world programming digest |
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:
1 2 3 4 5 6 7 8 |
class demo { public static void main(String[] args) { System.out.println("Hello "); System.out.println("World "); System.out.println("programming digest"); } } |
Output:
1 2 3 |
Hello World programming digest |
Example: how to print literal and variable using output data method System.out.println():
1 2 3 4 5 6 7 |
class demo{ public static void main(String[] args) { Double var = -50.4; System.out.println(20); System.out.println(var); } } |
Output:
1 2 |
20 -50.4 |
Example: how to concatenate the two variables within the input method system.out.println():
1 2 3 4 5 6 7 8 9 |
class demo{ public static void main(String[] args) { Double number = -40.2; String a= Hello; String b= world; System.out.println(a + " " +b ); System.out.println( "Number = " + number); } } |
Output:
1 2 |
Hello World -40.2 |
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:
1 2 3 |
java .util .Scanner scan = new java .util .Scanner (System.in); System.out.print(" Enter something here: '); string s = scan .next (); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Scanner; class demo { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter data: "); int number = input.nextInt(); System.out.println("The Entered data is: " + number); input.close(); } } |
Output:
1 2 |
Enter data: 500 The Entered data is: 500 |
Example: how to enter string, double, and float values in java input and output methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class demo { public static void main(String[] args) { Scanner input_data = new Scanner(System.in); System.out.print("Enter float value: "); float myFloat = input_data.nextFloat(); System.out.println("Float entered = " + myFloat); System.out.print("Enter double value: "); double myDouble = input_data.nextDouble(); System.out.println("Double entered = " + myDouble); System.out.print("Enter text: "); String myString = input_data.next(); System.out.println("Text entered = " + myString); } } |
Output:
1 2 3 4 5 6 |
Enter float: 100.3 Float entered = 100.3 Enter double: -40.4 Double entered = -40.4 Enter text: Hello World! Text entered = Hello World! |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class rectangle { public static void main (string [] args ) { java .util . Scanner scan = new java .util .Scanner(System.in); string inputlength inputwidth ; System.out.print(" Specify the length: '); inputLength = scan .next (); System.out.print(" Specify the width: '); inputwidth = scan .next (); scan.close(); double width=Double.parseDouble ( inputLength ); double length=Double.parseDouble ( inputwidth ); double circumference =( length+width)*2; double surface =length*width; System.out. println(" Scope: " + scope); System.out. println("Surface: " + area); } } |
OutPut:
1 2 3 4 |
Specify the length: 17.2 Specify the width: 3.4 Scope: 41.199999999999996 Area: 58.48 |
Related Article:
Variables in Java Context: Validity level, Object Variables, Wrapper Classes, Class variables
Java Literal: Boolean, whole Numbers, Floating point numbers