Sub topic 3
Reading Numbers and Words
This chapter assumes basic Java syntax, classes, methods, variables, and control flow. You should also know how to compile and run a Java program and recognize types such as String, int, and double.
Creating a Scanner
A console program receives typed data through standard input. In Java, standard input is represented by System.in. A Scanner is a class that reads and converts that input into useful values.
Import Scanner from java.util and give it System.in:
import java.util.Scanner;
public class Profile {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("First name: ");
String firstName = input.next();
System.out.print("Age: ");
int age = input.nextInt();
System.out.print("Height in meters: ");
double height = input.nextDouble();
System.out.println(firstName + " is " + age
+ " years old and " + height + " meters tall.");
}
}Compile and run the program, then type values separated by spaces or line breaks:
Mina
28
1.72The program prints:
Mina is 28 years old and 1.72 meters tall.Scanner skips whitespace before reading a token. Whitespace includes spaces, tabs, and line breaks.
Choosing a Reading Method
A token is a separate piece of input surrounded by whitespace. The method you call determines how Java interprets the next token.
| Method | Result type | Example input |
|---|---|---|
| `next()` | `String` | `Mina` |
| `nextInt()` | `int` | `28` |
| `nextDouble()` | `double` | `1.72` |
| `nextLong()` | `long` | `9000000000` |
| `nextBoolean()` | `boolean` | `true` |
Use next() for one word or an identifier. Use a numeric method when the value should participate in arithmetic:
int quantity = input.nextInt();
double price = input.nextDouble();
double total = quantity * price;
System.out.println("Total: " + total);The numeric methods do more than return text. They parse the token and convert it to the requested primitive type. For example, nextInt() converts "28" into the integer value 28.
If the next token cannot be converted, the scanner reports an input error. For example, nextInt() cannot read twenty-eight. Input validation is important in interactive programs, but this chapter focuses on successful token reads.
How Scanner Consumes Input
The scanner maintains a position in the input stream. Each successful reading method consumes one token and moves that position forward.
flowchart LR
A["Input: Mina 28 1.72"] --> B["next()"]
B --> C["String name = Mina"]
C --> D["nextInt()"]
D --> E["int age = 28"]
E --> F["nextDouble()"]
F --> G["double height = 1.72"]The order of method calls must match the order of the values supplied. If the program calls nextInt() while the next token is a name, the read fails. If the program expects three tokens but receives only two, the final read waits for more input when running interactively.
A scanner reads tokens, not concepts. It does not know that a value is an age or a price. Your variable names and prompts provide that meaning.
Practice
When input contains a person's first name, number of completed courses, and average grade, read them in that order. A first name without spaces can be read with next(). A sentence or name containing spaces requires line-based input instead.
Exercise
What values do these statements read from the input Ada 3 91.5? String name = input.next(); int courses = input.nextInt(); double grade = input.nextDouble();
Show answer
name is "Ada", courses is 3, and grade is 91.5.