Input Output java Basics

ujjwal kumar
1 min readAug 22, 2022

The Scanner class is used to get user input, and it is found in the java.util package.

To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings:

Example

import java.util.Scanner;  // Import the Scanner classclass Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
String userName = myObj.nextLine(); // Read user input
System.out.println("Username is: " + userName); // Output user input
}
}

In the example above, we used the nextLine() method, which is used to read Strings. To read other types, look at the table below:

nextBoolean()Reads a boolean value from the user

nextByte()Reads a byte value from the user

nextDouble()Reads a double value from the user

nextFloat()Reads a float value from the user

nextInt()Reads a int value from the usernextLine()

Reads a String value from the usernextLong()

Reads a long value from the usernextShort()

Reads a short value from the user

Questions For Practice

  1. Java Input/Output | Practice | GeeksforGeeks
  2. Read from STDIN, Write to STDOUT | Practice Problems (hackerearth.com)
  3. Do stdout1 and stdout 2 fro hackerrank

--

--