Converting numerical values into words

Report a typo

You are given a partially completed Java program that converts a numeric value (1 or 2) into its equivalent word ("One" or "Two"). If an invalid value is entered, the program should print "Invalid number." However, some code blocks are missing. Your task is to fill the blanks in the Java code to ensure that the program functions as described.

Fill in the gaps with the relevant elements
public class Main {
    public static void main(String[] args) {
        int number = 2;
        String numberInWords;
         (number) {
             1:
                numberInWords = "One";
                break;
            case 2:
                numberInWords = "Two";
                ;
            :
                numberInWords = "Invalid number";
                break;
        }
        System.out.println(numberInWords);
    }
}
defaultbreakswitchcase

Create a free account to access the full topic