11
\$\begingroup\$

Given a String, if the String begins with "red" or "blue" return that color String, otherwise return the empty String

public String seeColor(String str) {
  if(str.length() <= 3){
    if(str.equals("red")){
      return "red";
    }
    return "";
  }
  else if(str.substring(0, 3).equals("red")){
    return "red";
  }
  else if(str.substring(0, 4).equals("blue")){
    return "blue";
  }
  return "";
}

I really don't like the fact that I'm using same code for "red" twice but I can't get to think out another way. If str is equal to 3, I have to make the check for "red", right? Example input: "redxx" - output "red", "xxred" - output "", "blueAvenue" - output "blue"

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You could add all your test conditions to a separate list, and then use startsWith. \$\endgroup\$
    – holroy
    Dec 11, 2015 at 22:41

3 Answers 3

14
\$\begingroup\$

Why aren't you using String.startsWith(prefix)? This should be a one-liner.

return str.startsWith("red")  ? "red"  :
       str.startsWith("blue") ? "blue" : "";
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Lol, because I did not knew that there is such a thing as String.startsWith. Sorry I am really new to programming. Next time I'll read first before asking stupid questions :) \$\endgroup\$ Dec 11, 2015 at 22:46
  • 10
    \$\begingroup\$ This was not a stupid question. No one expects you to know the entire set of functions in a language at the start. +1 for solving the problem before asking for a better approach. \$\endgroup\$
    – Tejas Kale
    Dec 12, 2015 at 10:02
  • \$\begingroup\$ @TejasKale, thank you! I think it's better to do your best and if you can - solve the problem even if it's not the best code you've made, AFTER that ask for better solution :) \$\endgroup\$ Dec 12, 2015 at 20:29
4
\$\begingroup\$

Here is a full example using a list to hold your colors, and Str.startsWith:

import java.io.*;
import java.util.*;

class Main {

    static final List<String> colors = Arrays.asList("red", "blue");

    static public String seeColor(String str) {
        for (String color : colors ) {
            if (str.startsWith(color)) {
                return color;
            }
        }
        return "";
    }


    public static void main(String[] args) {
        List<String> testColors = Arrays.asList("redxx", "xxred", "blueAvenue");

        for (String testColor: testColors) {
            System.out.println(testColor + " -> " + seeColor(testColor));
        }
    }
}

Note than when checking for multiple colors, you can only return if the for loop matches, and keep the empty return after the loop. In addition notice the simple but subtle naming differentiation between color and colors which makes it easy to distinguish between the list and a single element.

\$\endgroup\$
1
\$\begingroup\$

As @200_success said, String.startsWith is great and its use simplifies the code down to:

return str.startsWith("red") ? "red" : str.startsWith("blue") ? "blue" : "";

I used a nested ternary, but it still is very easy to undertand, and I do not fell like bloating this easy function with if and elif statements.

\$\endgroup\$
2
  • \$\begingroup\$ @TsenkoAleksiev I suggest you read ternary so that you can fully understand my answer \$\endgroup\$
    – Caridorc
    Dec 11, 2015 at 22:49
  • 1
    \$\begingroup\$ @Caridorc I know about ternary...I did not knew about String.startsWith :) \$\endgroup\$ Dec 11, 2015 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.