using java scanner code example

Example 1: java scanner

import java.util.Scanner;

// import scanner 

Scanner myScanner = new Scanner(System.in); // Make scanner obj

String inputString = myScanner.nextLine(); // Take whole line

boolean inputBoolean = myScanner.nextBoolean(); //Boolean input

long inputLong = myScanner.nextLong(); //Interger,long ... input

Example 2: how to use scanner class in java

// import Scanner
import java.util.Scanner;

// Initialize Scanner
Scanner input = new Scanner(System.in);

// Test program with Scanner
System.out.println("What is your name?");
String name = input.nextLine();

System.out.println("Hello," + name + " , it is nice to meet you!");

Example 3: static scanner java

import java.util.Scanner;


class ComputerScienceHomework16 {
  // making it static in other functions
  static Scanner scanner = new Scanner(System.in);
  
  public static void test(){
    System.out.print("type a number: ");
    int num = scanner.nextInt();
    System.out.println("you typed the number " + num);
  }
  
  public static void main(String[] args) {
    test()
    
  }
}

Tags:

Java Example