Skip to content
On this page

Input And Output

In a Java program, the JVM always executes statements ending with a semicolon ; in sequence by default. However, in actual code, programs often need to make conditional judgments and loops. Therefore, a variety of flow control statements are needed to implement program jumps and loops.

In this section we will introduce if conditional judgment, switch multiple selection and various loop statements.

Output

In the previous code, we always use System.out.println() to output something to the screen.

println is the abbreviation of print line, which means output and newline. Therefore, if you don’t want to wrap the output, you can use print() :

java
public class Main {
  public static void main(String[] args) {
    System.out.print("A,");
    System.out.print("B,");
    System.out.print("C.");
    System.out.println();
    System.out.println("END");
  }
}

Pay attention to the execution effect of the above code.

Formatted Output

Java also provides the function of formatting output. Why format the output? Because the data represented by computers may not be suitable for human reading:

java
public class Main {
    public static void main(String[] args) {
        double d = 12900000;
        System.out.println(d); // 1.29E7
    }
}

If you want to display the data in the format we expect, you need to use the formatted output function. Formatted output uses System.out.printf() . By using the placeholder %? printf() can format the following parameters into the specified format:

java
public class Main {
    public static void main(String[] args) {
        double d = 3.1415926;
        System.out.printf("%.2f\n", d); // Displays 3.14 to two decimal places
        System.out.printf("%.4f\n", d); // Display 3.1416 to 4 decimal places
    }
}

Java's formatting function provides a variety of placeholders that can "format" various data types into specified strings:

  • %d Formatted output integer
  • %x Formatted output hexadecimal integer
  • %f Formatted output floating point number
  • %e Formatted output of floating point numbers in scientific notation
  • %s Format string

Note that since % represents a placeholder, two consecutive %% represent a % character itself.

The placeholders themselves can also have more detailed formatting parameters. The following example formats an integer in hexadecimal, padding eight bits with zeros:

java
public class Main {
    public static void main(String[] args) {
        int n = 12345000;
        System.out.printf("n=%d, hex=%08x", n, n); // Note that two % placeholders must be passed in two numbers
    }
}

For detailed formatting parameters, please refer to the JDK document java.util.Formatter

Enter

Compared with output, Java's input is much more complicated.

Let's first look at an example of reading a string and an integer from the console:

java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // Create Scanner object
        System.out.print("Input your name: "); // Printing tips
        String name = scanner.nextLine(); // Read a line of input and get a string
        System.out.print("Input your age: "); // Printing tips
        int age = scanner.nextInt(); // Read a line of input and get an integer
        System.out.printf("Hi, %s, you are %d\n", name, age); // Formatted output
    }
}

First, we import java.util.Scanner through the import statement. import is a statement that imports a certain class and must be placed at the beginning of the Java source code. We will explain how to use import in detail in the Java package later.

Then, create a Scanner object and pass it into System.in . System.out represents the standard output stream, while System.in represents the standard input stream. Although it is possible to directly use System.in to read user input, it requires more complex code, and Scanner can simplify subsequent code.

With the Scanner object, to read the string entered by the user, use scanner.nextLine() , and to read the integer entered by the user, use scanner.nextInt() . Scanner automatically converts data types, so manual conversion is not necessary.

To test input, user input must be read from the command line. Therefore, the process of compilation and execution needs to be followed:

sh
$ javac Main.java

If there is a warning when this program is compiled, you can ignore it temporarily and explain it in detail later when you learn IO. After successful compilation, execute:

sh
$ java Main
Input your name: Bob ◀── Input Bob
Input your age: 12   ◀── Input 12
Hi, Bob, you are 12  ◀── Output

After entering a string and an integer respectively according to the prompts, we got the formatted output.

Summary

The output provided by Java includes: System.out.println() / print() / printf() , where printf() can format the output;

Java provides Scanner objects to facilitate input. To read the corresponding type, you can use: scanner.nextLine() / nextInt() / nextDouble()

Input And Output has loaded