Using Strings in a switch Statement - Java Language Basics

Java examples for Language Basics:switch

Description

Using Strings in a switch Statement

Demo Code

public class Main {
  public static void main(String[] args) {
    operate("on");
    operate("off");
    operate("ON");
    operate("Nothing");
    operate("OFF");
    operate("1");
    operate(null);//from   ww  w  .ja  v a 2  s.c  o  m
  }

  public static void operate(String status) {
    // Check for null 
    if (status == null) {
      System.out.println("status cannot be null.");
      return;
    }

    // Convert to lowercase 
    status = status.toLowerCase();

    switch (status) {
      case "on":
        System.out.println("Turn on");
        break;
      case "off":
        System.out.println("Turn off");
        break;
      default:
        System.out.println("Unknown command");
        break;
    }
  }
}

Result


Related Tutorials