-2

I'm writing a method to find the upper case letters in a given string. I have this

public static String FindUpperCase (String x){
 for (int i = x.length(); i>=0; i--){
      if (Character.isUpperCase(x.charAt(i))){
          return x.substring(i); }
    }

But I'm getting an error thats telling me I must return a string. When I look up on the API what substring does it tells me that it returns a string that is a subset of the other string...which means I am returning a string, right? I was told that it was because I am returning a string within the loop and that's not the same thing but I'm a little confused by what this means because isn't the loop in the method? Does anyone know what I'm doing wrong or how I can fix this?

guribe94
  • 1,551
  • 3
  • 15
  • 29
  • 1
    It's complaining because you're not (yet) returning a string if the `if` statement always evaluates to `false`. – Dennis Meng Oct 05 '13 at 18:39
  • 3
    Your code returns a string only when it enters the IF condition. What if the IF is never executed? – Vaibhav Desai Oct 05 '13 at 18:39
  • By "find uppercase letters" do you mean "portion of string after first upper case letter"? – arshajii Oct 05 '13 at 18:42
  • 1
    *All* execution paths must return a string. – Dave Newton Oct 05 '13 at 18:42
  • I would like to know why the downvotes to the question, just to be clear – Omar Mainegra Oct 05 '13 at 18:48
  • For downvotes, my guess is that that if you look at the actual error it will say that at the *end of the method* there is no String returned. I suspect reading the error carefully will reveal the problem. In my IDE and compiler it clearly shows the last line, the last `}` is the issue, not the line the OP is questioning. – Peter Lawrey Oct 05 '13 at 18:58

5 Answers5

4

No, you're not always returning a string. What if the input is entirely lower case?

Basically, you need a return statement (or throw an exception) after the for loop, to handle the situation where you get to the end of it. Even in cases that you can reason that you'll never actually get to the end of the loop, the Java compiler follows strict rules for reachability, as specified in section 14.21 of the JLS. So even if your return statement were unconditional and we know that length() always returns a non-negative value, this still wouldn't compile:

public static String broken(String input) {
    // *We* know that we'll always go into the body of the loop...
    for (int x = input.length(); x >= 0; x--) {
        return input;
    }
    // The end of the method is still reachable from the compiler's standpoint
}

The end of a non-void method can't be reachable - you must either throw an exception or return a value.

Also note that your initial value should be x.length() - 1 or x.charAt(i) will throw an exception. You should also change your method name to follow Java naming conventions.

Oh, and currently you're not returning "the upper case letters" - you're returning "everything from the last upper case character onwards" which is entirely different.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

You are returning subjected to a condition, so if the condition is never true you are not returning. Try instead.

public static String FindUpperCase (String x){
    for (int i = x.length() - 1; i>=0; i--){
        if (Character.isUpperCase(x.charAt(i))){
            return x.substring(i); }
    return "";
}

Also Java is indexes start at 0, so your for sentence starts at x.length() - 1 (last position) or you will get StringIndexOutOfBoundsException

Omar Mainegra
  • 4,006
  • 22
  • 30
1

Consider the case when there's no uppercase in the given string, in that case, the function won't return anything.

So just after your for loop, you can return an empty string to make the function declaration valid.

neeagl
  • 348
  • 1
  • 13
1

Because you return string only if Character.isUpperCase(x.charAt(i)) is true. You have to return for example empty string if it is false.

public static String FindUpperCase (String x){
   for (int i = x.length(); i>=0; i--){
      if (Character.isUpperCase(x.charAt(i))){
         return x.substring(i); }
   }
   return "";
}
Sylwek
  • 856
  • 1
  • 9
  • 24
1

Your problem is, that you are returning a string dependend on an if statement. You have to return a string or null in every possible case. Just place a return null or return "" at the last line of the function for a quick and dirty solution. Better would be to think what you want to return if no uppercase character is found.

Anon
  • 44
  • 4