Web Tutor

Other Entries

Input and Output in Java

This tutorial will show you how to read input from the keyboard, use that input to calculate a result, and then format that result for the output.
Java brings various streams with its I/O package that helps the user to perform all the input-output operations. These streams support all types of objects, data types, characters, files, etc to fully execute input-output operations.
 
The following are three standard or default streams that Java has to provide and are also most common in use.
 
  1. System.in - This is the standard input stream that is used to read characters from the keyboard or any other standard input device.
  2. System.out -This is the standard output stream that is used to produce the result of a program on an output device like the computer screen.
 
Here is a list of various print functions that are used to output statements.
 
print(): This is a method in java that is used to print text on the console. The text is passed as a parameter to this method in form of a string. When the method prints the text on the console, the cursor remains at the end of the text.
 
Syntax
System.out.print(parameter);

Example

package PrintDemo;

public class PrintDemo {

	public static void main(String[] args) {
		
        System.out.print("Hello Java  ");
        System.out.print("My first Java program  ");
        System.out.print("Am looking forward to learning further  ");
	}

}

 

Output

450_44166e577c4d5aae939cd54ed808894e.png

println(): This method is also used to display a text on the console. It print the text on the console and the cursor moves to the start of the next line at the console. The next printing takes place from the next line.

Syntax

System.out.println(parameter);

Example

package PrintDemo;

public class PrintDemo {

	public static void main(String[] args) {
		
        System.out.println("Hello Java");
        System.out.println("My first Java program");
        System.out.println("Am looking forward to learning further");
	}

}

Output

450_3ee33d438b91d1ab4b577a4e719e8147.png

printf(): This method is similar to printf of of C. Note that System.out.print and System.out.println takes a single argument, but printf may take multiple arguments. This is used to format the output in Java.

Example

package PrintDemo;

public class PrintDemo {

	public static void main(String[] args) {
		int x = 100;
		
        System.out.printf("Printing simple"+" integer = %d\n",x);
        System.out.printf("Formatted with"+" precision: PI= %.2f\n",Math.PI);
        
        float n = 5.2f;
        System.out.printf("Formatted to "+"specific width: n = %.4f\n",n);
	}

}

 

Output

450_d251557d1eadf64c8da3e59809e59ff3.png

3. System.err -This is a standard error stream that is used to output all the error data that a program might throw, on a computer screen or any standard output device. The stream also uses all the three functions mentioned above to output error. print(), println() and printf()

 

Posted in Java on September 10 2022 at 11:18 AM

Comments (0)

No login

Popular Blogs