High School Physics

Java program – how to use the Scanner class to capture user input

Here is a Java program that helps you to understand how to use the Scanner class to capture user input. The Scanner class is used to get user input, and it is found in java.util package.

Using Scanner class in Java program (code of the program)

import java.util.Scanner;

public class ScannerTest //public main class, to be saved in a file with .java
public static void main(String args[])

Scanner keyboard=new Scanner(;
System.out.println(“###Enter the number of teams followed by”);
System.out.println(“the number of players per team###” );
int num_teams=keyboard.nextInt();
int num_players=keyboard.nextInt();

int tot_num_players=num_teams*num_players;
System.out.println(“Tournament is participated by “+ tot_num_players +” players.”);



See also  Java program with String and StringBuffer
Scroll to top