Only Integer-type Values

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

The value in the case and the labels in each case must be of integer type (including char).

Only Integer-type Values

If you need to select among several options based on complicated requirements, use nested if statements, or if else if statments (which are really the same thing.) Here is the previous program, rewritten with equivalent if else if statments.

import java.io.*;
class Switcher
{
  public static void main ( String[] args ) throws IOException
  {
    String lineIn;
    char   color  ;    
    String message = "Color is";
    
    BufferedReader stdin = new BufferedReader
      ( new InputStreamReader(System.in) );

    System.out.println("Enter a color letter:");
    lineIn = stdin.readLine();
    color = lineIn.charAt( 0 );  // get the first character

    if      ( color=='r' || color=='R' )    
      message = message + " red" ;

    else if ( color=='o' || color=='O' )               
      message = message + " orange" ;
               
    else if ( color=='y' || color=='Y' )               
      message = message + " yellow" ;
               
    else if ( color=='g' || color=='G' )               
      message = message + " green" ;
               
    else if ( color=='b' || color=='B' )               
      message = message + " blue" ;

    else if ( color=='v' || color=='V' )               
      message = message + " violet" ;

    else 
      message = message + " unknown" ;
            
    System.out.println ( message ) ;
  }
}

QUESTION 12:

Is there something wrong with the program? Is == being used correctly?