The value in the case and the labels in each case
must be of integer type (including char).
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 ) ;
}
}
Is there something wrong with the program?
Is == being used correctly?