Interviews - C# Corner

Mahesh Chand
How to read input from console using Scanner in Java?
By Mahesh Chand in Java onAug 07 2019
  • Rohit Gupta
    Aug, 2019 7

    Java Scanner is a special Java class that is used to take inputs from all kinds of inout streams. The system console, files, StringBuilder, StringBuffers are to name a few.

    The following code snippet gets input from system console and reads it using Java Scanner utility class.

    1. import java.util.Scanner;
    2. /**
    3. * An example program to read a String from console input in Java
    4. */
    5. public class CSharpCorner
    6. {
    7. public static void main(String[] args)
    8. {
    9. System.out.print("Enter a string : ");
    10. Scanner scanner = new Scanner(System.in);
    11. String inputString = scanner. nextLine();
    12. System.out.println("String read from console is : \n"+inputString);
    13. }
    14. }

    ‘System.in’ means console, so in the above code we are telling Java compiler to accept input from the console, and save the accepted input into a String variable named ‘inputString’

    For a detailed tutorial on Java Scanner, visit
    https://www.c-sharpcorner.com/article/java-scanner-tutorials/

    • 1

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS