Home Java Tutorial Java Basic Input and Output

Java Basic Input and Output

by Anup Maurya
55 minutes read

Java Basic Input and Output (I/O) is a crucial aspect of Java programming language. Input and output operations are used to communicate with the user. In this tutorial, we will discuss simple ways to display output to users and take input from users in Java.

Output in Java

Output operations in Java are used to display data to the user, store data in a file, or send data to a database. The following are the common ways to display output in Java on console.

System.out.println()

The System.out.println() method is used to display output on the console. The following code snippet shows how to use this method.

class MyClass {
   public static void main(String[] args) {
      System.out.println("Hello World");
   }
}

In the above code, we have used the System.out.println() method to display the “Hello World” message on the console.

System.out.print()

The System.out.print() method is used to display output on the console without a newline character. The following code snippet shows how to use this method.

class MyClass {
   public static void main(String[] args) {
      System.out.print("Hello");
      System.out.print("World");
   }
}

Input in Java

Input operations in Java are used to read data from the user, files or databases. However, in this tutorial, you will learn to get input from user using the object of Scanner class.

In order to use the object of Scanner, we need to import java.util.Scanner package.

Scanner Class

The Scanner class is a part of the java.util package and provides various methods to read different types of input such as int, double, float, String, etc.

To use the Scanner class, we need to create an object of the Scanner class by passing the input stream as the argument. The following code snippet shows how to use the Scanner class to take input from the user.

import java.util.Scanner;

// create an object of Scanner
Scanner input = new Scanner(System.in);

// take input from the user
int number = input.nextInt();

Get 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);

        // closing the scanner object
        input.close();
    }
}

Output

Enter an integer: 23
You entered 23

In the above example, we have created an object named input of the Scanner class. We then call the nextInt() method of the Scanner class to get an integer input from the user.

Similarly, we can use nextLong()nextFloat()nextDouble(), and next() methods to get longfloatdouble, and string input respectively from the user.

Get float, double and String Input

import java.util.Scanner;

class Input {
    public static void main(String[] args) {
    	
        Scanner input = new Scanner(System.in);
    	
        // Getting float input
        System.out.print("Enter float: ");
        float myFloat = input.nextFloat();
        System.out.println("Float entered = " + myFloat);
    	
        // Getting double input
        System.out.print("Enter double: ");
        double myDouble = input.nextDouble();
        System.out.println("Double entered = " + myDouble);
    	
        // Getting 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!

related posts

Leave a Comment