codegurukool.com · Free coding tutorials & online compiler

Home › Java Tutorial › Java user input

Java user input

Read data with Scanner.

Create a Scanner on System.in to read what the user types: nextLine(), nextInt(), nextDouble(). This page has no keyboard input, so the example reads from a String instead; it works exactly the same way.

Example code

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    // Real program: new Scanner(System.in)
    Scanner sc = new Scanner("Ada 36");
    String name = sc.next();
    int age = sc.nextInt();
    System.out.println(name + " is " + age);
  }
}

Run this code in the online editor

← Java lambdas and streamsJava file handling →