How To Give Input In Java Using Scanner Code Example

Snippet 1

  import java.util.scanner;

Scanner *scanner name* = new Scanner(*input source*);

// Examples:
Scanner user = new Scanner(;
Scanner text = new Scanner(new File("text.txt")); 

Snippet 2

  import java.util.Scanner;  // Import the Scanner class

class MyClass {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(;  // 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

Similar Snippets

Creating Java Main Method Code Example – java

Find Duplicates In Arraylist Java Code Example – java

Java Creat A Folder Code Example – java

Firestore Find Doc And Set Data Code Example – java

Create Copy Of Array From Another Array Code Example – java

Copyright © Code Fetcher 2020




Leave a comment

Your email address will not be published. Required fields are marked *