Number to Word Using Switch in Java
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.
Java Program
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
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.
- 1
String.valueOf(num).toCharArray()turns the number into its digit characters, visited in left-to-right order. - 2For each character,
ch - '0'converts it back to its numeric digit value using ASCII arithmetic. - 3
switch (digit)matches that digit against cases 0 through 9, each assigning the matching word. - 4Each word is appended to a
StringBuilder, separated by spaces, building the final result.
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.
Why: Each of the number's d digits triggers one switch dispatch, and the result string grows by one word per digit.
Key Concepts
Approach 2: Java 8
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
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.
- 1
WORDSis aString[]holding "Zero" through "Nine" at indices 0 through 9. - 2
String.valueOf(num).chars()streams the number's digit characters. - 3
.mapToObj(c -> WORDS[c - '0'])converts each character to its digit value and looks up the matching word. - 4
.collect(Collectors.joining(" "))joins every word with a single space between them.
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.
Why: The stream still visits each of the d digits once, and Collectors.joining() builds a result string proportional to the digit count.