English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input/output (I/O)

Java Reader/Writer

Other Java topics

Java Input and Output

In this tutorial, you will learn the simple methods of displaying output to the user and obtaining input from the user in Java.

Java output

In Java, you can simply use

System.out.println(); or
System.out.print(); or
System.out.printf();

sends output to standard output (screen).

Here,

  • System is a class

  • out is a public static field: it accepts output data.

If you are not familiar with it, do not worry. We will discuss class, public, and static in the following chapters.

Let's take the example of outputting a line.

class AssignmentOperator {
    public static void main(String[] args) {
    	
        System.out.println("Java programming is interesting.");   
    }
}

Output:

Java programming is interesting.

Here, we use println() to display the string method.

The differences between println(), print(), and printf()

  • print() - It prints the string within the quotes.

  • println() - It prints the string within the quotes, similar to the print() method. Then the cursor moves to the beginning of the next line.

  • printf() - Tt provides string formatting (similar to C / C ++Programming with printf).

Example: print() and println()

class Output {
    public static void main(String[] args) {
    	
        System.out.println("1. println ");
        System.out.println("2. println ");
    	
        System.out.print("1. print ");
        System.out.print("2. print");
    }
}

Output:

1. println 
2. println 
1. print 2. print

In the above example, we demonstrated how the print() and printf() methods work.

class Variables {
    public static void main(String[] args) {
    	
        Double number = -10.6;
    	
        System.out.println(5);
        System.out.println(number);
    }
}

When running the program, the output is:

5
-10.6

Here, you can see that we did not use quotes. This is because we did not use quotes to display integers, variables, and so on.

class PrintVariables {
    public static void main(String[] args) {
    	
        Double number = -10.6;
    	
        System.out.println("I am " + "awesome.");
        System.out.println("Number = ") + number);
    }
}

Output:

I am awesome.
Number = -10.6

In the above example, please note the following line:

System.out.println("I am " + "awesome.");

Here, we use+The operator is used to concatenate (join) two strings: "I am " and "awesome.".

System.out.println("Number = ") + number);

Here, first get the value of the variable number. Then connect the value to the string: "Number =".

Java input

Java provides different methods to obtain input from the user. However, in this tutorial, you will learn to obtain input from the user using the Scanner class object.

To use the object Scanner, we need to import the java.util.Scanner package.

import java.util.Scanner;

For more information about Java import packages, please visitJava import package.

Then, we need to create an object of the Scanner class. We can use this object to get input from the user.

//Create Scanner object
Scanner input = new Scanner(System.in);
//Accept user input
int number = input.nextInt();

Example: Obtain integer input from the user

import java.util.Scanner;
class Input {
    public static void main(String[] args) {
    	
        Scanner input = new Scanner(System.in);
    	
        System.out.print("Enter an integer: ");
        int number = input.nextInt();
        System.out.println("You entered "); + number);
        //Close the scanner object
        input.close();
    }
}

Output:

Enter an integer: 23
You entered 23

In the above example, we created an object named input of the Scanner class. Then, we called the nextInt() method of the Scanner class to obtain integer input from the user.

Similarly, we can use nextLong(), nextFloat(), nextDouble(), and next() methods to obtain long, float, double, and string inputs from the user, respectively.

Note:We have used the close() method to close the object. It is recommended to close the scanner object once the input is completed.

Example: Get floating-point, double-precision, and string input

import java.util.Scanner;
class Input {
    public static void main(String[] args) {
    	
        Scanner input = new Scanner(System.in);
    	
        //Get float input
        System.out.print("Enter float: ");
        float myFloat = input.nextFloat();
        System.out.println("Float entered = "); + myFloat);
    	
        //Get double input
        System.out.print("Enter double: ");
        double myDouble = input.nextDouble();
        System.out.println("Double entered = ", + myDouble);
    	
        //Get String Input
        System.out.print("Enter text: ");
        String myString = input.next();
        System.out.println("Text entered = ", + myString);
    }
}

Output:

Enter float: 2.343
Float entered = 2.343
Enter double: -23.4
Double entered = -23.4
Enter text: Hey!
Text entered = Hey!

As mentioned earlier, there are several other methods to obtain input from the user. For more information on Scanner, please visitJava Scanner.