1

i am trying to create switch statements for an address parser, that parses a String through a regex, but i am having difficulties with it.

My challenge is this part:

if(m.matches() && i==0){
            b.street(m.group(1));
            break;

how to make this into

switch(SOMETHINGHERE) {
case SOMETHINGHERE: SOMECODE HERE
}

here's the regex and the patternList, which is an Arraylist (or Linkedlist, i haven't decided:

static String streetReg = "([a-zæøåäöëüéèA-ZÆØAÄÖËÜÉÈ -./]*)";
static String symbolsReg = "[ ,.-]*";

public static void addPatterns() {
        patternList.add(Pattern.compile(streetReg + "" + symbolsReg));
    }

how do i convert the following into switch statements?

public static Address parse(String s) {
    addPatterns();
    Builder b = new Builder();
    boolean noMatch = false;
    for(int i = 0; i<patternList.size(); i++){
        Matcher m = patternList.get(i).matcher(s);

        if(m.matches() && i==0){
            b.street(m.group(1));
            break;
        }
        else if(m.matches() && i==1){
            b.street(m.group(1));
            b.city(m.group(2));
            break;
        }else if(m.matches() && i==2) {
            b.postcode(m.group(1));
            b.city(m.group(2));
            break;
        }else if(m.matches() && i== 3){
            b.street(m.group(1));
            b.house(m.group(2));
            b.city(m.group(3));
            break;

i've tried to do this:

        switch (s){
            case "st":
                b.street(m.group(1));
                break;
            case "street and house":
                b.street(m.group(1));
                b.street(m.group(2));
                break;
            case "noMatch":
                noMatch =true;
                break;
        }
    }
if(noMatch)return null;
else return b.build();
}

but i get an error that the type chosen for the switch and the case don't match. e.g. boolean vs int or string vs int..

1 Answers1

2
if ( m.matches()) {
 switch(i) {
    case 0: b.street(m.group(1));
            checkNewVariable = true;
            break; // WARNING!! this break is a break for the switch, not for the FOR loop
    // you'll need to add a variable (checkNewVariable) so you can break after the switch if needed
   case 1: b.street(m.group(1));
           b.city(m.group(2));
           checkNewVariable = true;
           break;
// ... rest of your cases

  }

  if ( checkNewVariable ) { break; } // to break out of the for loop
}

Is an option.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • i will try! i was unsure what parameter the switch takes (s or m.matches() or i) !! – leroyjenkins Mar 21 '19 at 08:31
  • would it be possible to make the public static void addPatterns() { patternList.add(Pattern.compile(streetReg + "" + symbolsReg)); } as a linkedlist instead of an arraylist? – leroyjenkins Mar 21 '19 at 08:34
  • I would assume yes. – Stultuske Mar 21 '19 at 08:34
  • the switch can take s as param, but not m.matches() (this returns a boolean). i is also an option. I just went from your original code, and based myself on that. From your code, I can see what values of i you check on currently, not so much for s. – Stultuske Mar 21 '19 at 08:36
  • i am basically trying to make this function match an input String with different patterns. If a pattern matches it will create an Address object with the information given. – leroyjenkins Mar 21 '19 at 08:40
  • a matching pattern, or an identical String? they are far from the same. – Stultuske Mar 21 '19 at 08:41
  • trying to match an address input String by a user (i'm creating a map application) in different combinations. e.g. street house floor side postcode city – leroyjenkins Mar 21 '19 at 08:53
  • You'll have to be more specific about how they can be mapped/occur – Stultuske Mar 21 '19 at 09:35