generic scanner java code example

Example 1: scanner java

Scanner sc = new Scanner(System.in);
     int i = sc.nextInt();

Example 2: 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