Java ProgramsControl FlowNumber to Word Using Switch

Number to Word Using Switch in Java

intermediate·  Control Flow  ·  Switch Statement

Problem

Each digit from 0 to 9 has a fixed word form, so converting a whole number to words is just running the same switch lookup once per digit.

Given a number, convert each of its digits into its word form, in order.

Input
123
Output
One Two Three

Java Program

Java
public class NumberToWordSwitch { public static void main(String[] args) { int num = 123; StringBuilder result = new StringBuilder(); for (char ch : String.valueOf(num).toCharArray()) { int digit = ch - '0'; String word; switch (digit) { case 0: word = "Zero"; break; case 1: word = "One"; break; case 2: word = "Two"; break; case 3: word = "Three"; break; case 4: word = "Four"; break; case 5: word = "Five"; break; case 6: word = "Six"; break; case 7: word = "Seven"; break; case 8: word = "Eight"; break; case 9: word = "Nine"; break; default: word = ""; // unreachable in practice — every digit character maps to 0-9 } if (result.length() > 0) result.append(" "); result.append(word); } System.out.println(result); } }

Output

One Two Three

Core Logic

Extracting each digit from the number's string form and running it through a switch lookup builds the word-by-word result one digit at a time.

How It Works
  1. 1String.valueOf(num).toCharArray() turns the number into its digit characters, visited in left-to-right order.
  2. 2For each character, ch - '0' converts it back to its numeric digit value using ASCII arithmetic.
  3. 3switch (digit) matches that digit against cases 0 through 9, each assigning the matching word.
  4. 4Each word is appended to a StringBuilder, separated by spaces, building the final result.
For 123, the digits 1, 2, 3 are visited in order, and the switch resolves them to "One", "Two", "Three", joined into "One Two Three".
💡

Key Point: This spells out digits individually ("One Two Three"), not a full number name like "one hundred twenty-three" — each digit is converted independently of its position in the number.

Complexity
Time Complexity: O(d)Space Complexity: O(d)

Why: Each of the number's d digits triggers one switch dispatch, and the result string grows by one word per digit.

Key Concepts

switch statementdigit extractionStringBuilder

Approach 2: Java 8

Java
import java.util.stream.Collectors; public class NumberToWordStream { public static void main(String[] args) { int num = 123; String[] WORDS = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; // Maps each digit character to its word and joins them with a space String result = String.valueOf(num).chars() .mapToObj(c -> WORDS[c - '0']) .collect(Collectors.joining(" ")); System.out.println(result); } }

Output

One Two Three

Core Logic

A fixed array of the ten digit-words turns the switch lookup into a simple index, which a stream can apply to every digit at once.

How It Works
  1. 1WORDS is a String[] holding "Zero" through "Nine" at indices 0 through 9.
  2. 2String.valueOf(num).chars() streams the number's digit characters.
  3. 3.mapToObj(c -> WORDS[c - '0']) converts each character to its digit value and looks up the matching word.
  4. 4.collect(Collectors.joining(" ")) joins every word with a single space between them.
For 123, the stream maps '1', '2', '3' to "One", "Two", "Three" and joins them into "One Two Three".
💡

Key Point: The array lookup replaces the switch's ten cases with a single indexed access, which is what makes expressing this as a one-line stream pipeline practical.

Complexity
Time Complexity: O(d)Space Complexity: O(d)

Why: The stream still visits each of the d digits once, and Collectors.joining() builds a result string proportional to the digit count.

Key Concepts

Streamchars()Collectors.joining()

Related Programs