Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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"

share|improve this question
1  
You could add all your test conditions to a separate list, and then use startsWith. – holroy yesterday

3 Answers 3

up vote 12 down vote accepted

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

return str.startsWith("red")  ? "red"  :
       str.startsWith("blue") ? "blue" : "";
share|improve this answer
1  
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 :) – Tsenko Aleksiev yesterday
8  
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. – Tejas Kale 19 hours ago
    
@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 :) – Tsenko Aleksiev 8 hours ago

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.

share|improve this answer

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.

share|improve this answer
    
Thank you that answered my next question :) – Tsenko Aleksiev yesterday
    
@TsenkoAleksiev I suggest you read ternary so that you can fully understand my answer – Caridorc yesterday
1  
@Caridorc I know about ternary...I did not knew about String.startsWith :) – Tsenko Aleksiev yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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