Switch statement for String

edited July 2014 in How To...


As long as in Processing string variables could not be compared with "==" but with "equals()" function, could anyone tell me how to use switch statement for string variable?

Best regards, Paul



  • Answer ✓

    you need to use if else

    if (str.equals("one")) {
      // one
    } else if (str.equals("two")) {
      // two
    } else {
      // default
  • Thanks koogs, I was afraid to hear that, because I have to write statement for about 15 conditions :(

  • When (if?) Processing will support Java 8 syntax and semantics, we will be able to switch on strings.

    An alternative can be to use HashMap, but it would be convoluted for most usages...

  • _vk_vk
    edited July 2014

    What about Enum?

    There is this example PhiLho shared once:


    Humm, thinking again, I think this is no good, is the other war around, right? sorry

  • edited July 2014

    It's not that Processing's API itself isn't compatible w/ Java 8.
    I've heard reports in this very forum that Processing compiles w/ Java 8 just fine!

    Blame falls on Processing's IDE. More specifically, Processing's pre-processor! :-L
    It's not even completely compatible w/ Java 5 yet! :-&

    For example, we can't use enum nor having variables, functions & classes using international character names!
    Another 1, we can't define generics when using types w/ dots:
    java.util.List<PVector> myList;, for (Map.Entry<String, PVector> pairs) {}, etc.

    For Java 7, as already mentioned, we can't use String as a switch argument!
    Neither try w/ resources nor multiple catch exceptions!

    Also, literals w/ underline separators: 150_900_544. And binary literals: 0xb10001111!

    Those are what I remember outta my hat! And Java 8, don't get me even start about it. No chance! ~X(
    If we wanna use any of those Java features, we gotta use another bloated IDE! :-S
    I'd rather prefer that Processing's devs would fix Processing's pre-processor before doing a version 3! [-(

    P.S.: Just remembered, the empty diamond <> generic auto-complete doesn't work in P5 either:
    List<PVector> vecs = ArrayList<>();

  • Speaking of multiple exception catches - I use it in the same program and it works fine. I think IDE uses function instead of direct comparing to avoid char array or type comparison, which is present in PHP(for example). Enums are not similar to enums I used to write in other programming languages, so they are not applicable for my problem

  • edited July 2014

    For Java 7, as already mentioned, we can't use String as a switch argument!

    where was this mentioned? :-/

    i am on linux and have symlinked java folder with java 7..


    .i was under the impression that i could use it.

    so even someone uses java 7 that supports switch with strings , he cannot use it in processing? :-/

  • edited July 2014

    I'm on Lubuntu! I've simply erased "/java" subfolder. Only Windows needs a hardlink junction!
    Well, why don't you try those features out and see for yourself? :>

  • edited July 2014

    instead of if else

    if (str.equals("one")) {
      // one
    } else if (str.equals("two")) {
      // two
    } else {
      // default

    can't you like make an array of all words you need and loop over them and get an index as int as a result?

    then when you have the int, just use switch on it

    Best, Chrisir ;-)

  • you'd still have to write 15 case statements... "if else" isn't that much more verbose (although it depends on what the 15 things are, there may be a way of optimising that)

  • edited July 2014

    @GoToLoop i tried it and processing says "Cannot switch on a value String for source level below 1.7" although i use 1.7.0_60 i guess there so particular reason to use java 7

  • edited July 2014

    I've already clearly accused Processing's pre-processor as the boulder blocking Java's full compliance!
    Re-read my 1st reply above. We gotta petition Java 7 compatibility ASAP and Java 8 and on soon! >-)

  • edited July 2014

    @Chrisir, Not sure if creating string array, looping through it and switching integer would be more optimal than if-else statement :-? Don't forget about "Keep It Simple", so I prefer second:-)

  • I agree...

    But I thought since he wanted to use switch() the approach with an array combined with a for-loop might be the most elegant and short way to get there...


  • edited July 2014

    Speaking of optimization, should you know the expected frequency of the strings, ordering the if statements with the most frequent first is the way to go.

    If you are determined to use a switch/case approach and you have a limited word list you could create a unique hash for the corpus. then switch(hash(word)) would work once you coded the cases by the hash value for each word in to corpus.

    But if ... else if ... else if would be easier as already stated.

  • I know this is an old thread but I'm having the same situation.

    Would you need to put the series of if statements inside void loop() or can they be inside void serialEvent()?

  • there's no loop in processing, just draw

    but better use serialEvent

  • and use if ............else if............

Sign In or Register to comment.