Input / output

With the help of I/O routines the programmer can interact with the program. Among other things, he can query the program status or control the program flow with inputs. The simplest screen output is handled in Java as follows:
System.out.print() or System.out.println()

Variables can be appended to the output string using the + operator.

Output in Java

public class OutputStream {
	public static void main(String[] args) {
		System.out.print("Output ");
		System.out.println("stream");
		System.out.println("new line");
		System.out.printf("new line\n");
		System.out.println(4 + 5);
		System.out.println("Number: " + 6);
	}
}
Output
Output stream
new line
new line
9
Number: 6

Example java output

Code Explanation

LineDescription
3Declares and initializes the variables a and b of type int with the values 2 and 4
5The println() method outputs a string. The + operator can be used to represent variable values.
public class OutputStream {
	public static void main(String[] args) {
		int a = 2, b = 4;

		System.out.println("First number " + a + ", second number: " + b);
	}
}
Output
First number 2, second number: 4

Input in Java

With the help of the java Scanner class screen inputs can be read in Java. By creating an object of the Scanner class, you have the possibility to take the input value of certain data types over this object. If an object of the Scanner class is no longer needed, it should be closed with the close() method. Otherwise, a memory space is reserved until the end of the program.

Scanner input = new Scanner(System.in);

int val = input.nextInt();
String str = input.next();
char c = input.next().charAt(2);
double d = input.nextDouble();
float f = input.nextFloat();
boolean b = input.nextBoolean();
...

Example java input

Code Explanation

LineDescription
5Creates an object of type Scanner named input with predefined standard input object (System.in)
6The methods of the Scanner class can be accessed through the previously created Scanner object. nextInt() waits for user input. Then we store the input in the variable val
8Outputs the entered number
import java.util.Scanner;

public class InputStream {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int val = input.nextInt();

		System.out.println("Entered value: " + val);
	}
}
Output
5
Entered value: 5

Mixed Example

Code Explanation

LineDescription
5Creates an object of type Scanner named input with predefined standard input object (System.in)
6Creates the variables a, b and result of type integer
8 – 9Prompts the user to enter a number. The methods of the Scanner class can be accessed via the previously created Scanner object (input). nextInt() waits for a user input. Then we store the input in variable a
11 – 12Prompts the user to enter a second number, which is then stored in variable b
14Closes the Scanner object input and terminates user input
16Stores the sum of variable a and b in the variable result
18Outputs the result including the calculation
import java.util.Scanner;

public class InputOutput {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int a, b, result;

		System.out.print("Please enter first number: ");
		a = input.nextInt();

		System.out.print("Please enter second number: ");
		b = input.nextInt();

		input.close();

		result = a + b;

		System.out.println(a + " + " + b + " = " + result);
	}
}
Output
Please enter first number: 5
Please enter second number: 8
5 + 8 = 13