News for hackers. Stuff that matters.
Re:Java 7 New Features by alexatkeplar on Sunday December 23, 2012 @06:41AM (#4959252)

  > There have been countless times when I wished I could
  > have just used a String in a switch statement. When I
  > hit this block when coding, I’m forced by Java to refactor
  > my code to use a primitive datatype in my switch statement
I tend to use an enum instead, as it keeps the meaning clearer. For his example:

    private static enum StringFields { ADDO_QUAYE, BOATENG_NICOLAS, DEFAULT }
    try {
      field = StringFields.valueOf(fullName.replace(' ', '_').toUpperCase());
    } catch(IllegalArgumentException e) {
      field = StringFields.valueOf("DEFAULT");
    }

    switch (field) {
      case ADDO_QUAYE:
        System.out.println("He is the SRC president");
        break;
      case BOATENG_NICOLAS:
        System.out.println("He is the SU president");
        break;
      case default:
        System.out.println("He is no body!");
        break;
    }

There are no comments on this comment yet.