Tsallaka zuwa abun ciki
Justice Tech School

Reading Input with Scanner

Scanner reads from System.in or from a string.

Example
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner("42 Ada 3.5");
    int n = sc.nextInt();
    String name = sc.next();
    double d = sc.nextDouble();
    System.out.println(n + " " + name + " " + d);
  }
}

Result

42 Ada 3.5

Exercise

Read an int.

sc.()

Quiz Test

Which package has Scanner?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.