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
Code Explanation

This code defines a class called OutputStream that contains a main method. The main method uses various methods to output text to the console.

Here’s a step-by-step explanation of the code:

  1. The code defines a class called OutputStream using the class keyword.
  2. The main method is declared as public static void, which means it’s a public method that doesn’t return a value and can be invoked without creating an instance of the OutputStream class. The method takes an array of strings, args, as its argument, which contains the command-line arguments passed to the Java program when it’s run.
  3. Within the main method, the System.out.print method is called twice to output the text “Output ” and “stream” to the console.
  4. The System.out.println method is called to output the text “new line” to the console.
  5. The System.out.printf method is called to output the text “new line” to the console, followed by a newline character.
  6. The System.out.println method is called to output the result of the expression 4 + 5, which is 9, to the console.
  7. The System.out.println method is called to output the text “Number: 6” to the console.

Code Example: java output

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
Code Explanation

This code defines a class called OutputStream that contains a main method. The main method uses various methods to output text to the console.

Here’s a step-by-step explanation of the code:

  1. The code defines a class called OutputStream using the class keyword.
  2. The main method is declared as public static void, which means it’s a public method that doesn’t return a value and can be invoked without creating an instance of the OutputStream class. The method takes an array of strings, args, as its argument, which contains the command-line arguments passed to the Java program when it’s run.
  3. Within the main method, two integer variables a and b are declared and initialized to 2 and 4 respectively.
  4. The System.out.println method is called to output the string “First number ” followed by the value of a, followed by the string “, second number: ” followed by the value of b. The string concatenation is performed using the “+” operator.

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();
...

Code Example: java input

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
Code Explanation

This code defines a class called InputStream that contains a main method. The main method demonstrates how to get input from the user in a Java program.

Here’s a step-by-step explanation of the code:

  1. The code starts with an import statement to import the java.util.Scanner class. This class provides methods for reading input from the standard input stream, which is usually the keyboard.
  2. The code defines a class called InputStream using the class keyword.
  3. The main method is declared as public static void, which means it’s a public method that doesn’t return a value and can be invoked without creating an instance of the InputStream class. The method takes an array of strings, args, as its argument, which contains the command-line arguments passed to the Java program when it’s run.
  4. Within the main method, a Scanner object input is created by calling the constructor new Scanner(System.in). The System.in constant refers to the standard input stream, which is usually the keyboard.
  5. The input.nextInt() method is called to read an integer value from the user and store it in the variable val.
  6. The System.out.println method is called to output the string “Entered value: ” followed by the value of val. The string concatenation is performed using the “+” operator.

When this program is run, it will prompt the user to enter an integer value, which will be read using the input.nextInt() method. The program will then output the string “Entered value: ” followed by the entered value.

Code Example: input and output

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
Code Explanation

This code demonstrates how to perform input and output operations in Java.

The code starts by importing the Scanner class from the java.util package. The Scanner class provides a method for reading input from the keyboard.

In the main method, a Scanner object named “input” is created, which is used to read input from the standard input stream (the keyboard).

Two integer variables, “a” and “b”, are declared and initialized by reading the input from the keyboard using the nextInt() method of the Scanner class. The user is prompted to enter the values by printing a message to the console.

After the input is read, the Scanner object is closed using the close() method. This is good practice because it prevents resource leaks.

Finally, the sum of the two numbers is calculated and printed to the console along with the input values.