0Pricing
Java Academy · Lesson

Reading Numbers and Lines

nextInt, nextLine and pitfalls.

Reading Typed Numbers

Scanner can read numbers directly with methods like nextInt and nextDouble. They convert the next token into the matching primitive type.

Reading an Integer

nextInt() reads the next token and returns it as an int. We use a fixed string here so it runs without typing.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner("25");
        int age = scanner.nextInt();
        System.out.println(age + 1);
        scanner.close();
    }
}

All lessons in this course

  1. Reading Input with Scanner
  2. Reading Numbers and Lines
  3. Validating User Input
  4. BufferedReader Alternative
← Back to Java Academy