-2

this is the code I used to compare the chars in String that is input to "" and then would also check the chars before and after "" to see if they are similar in order to return true or false, however the code has an error that says a return value is missing, which i have no clue why?

import java.util.Scanner;
import java.util.Arrays;
public class everyOtherLetter {

    public static void main(String[] args) {

        Scanner y = new Scanner(System.in);

        System.out.println("type a word");

        String input = y.next();

        char[] t = input.toCharArray();

        System.out.println(Arrays.toString(t));

        //calling the function for even numbers
        //everyOtherLetter(input, true);

        //calling the function for odd numbers
        //everyOtherLetter(input, false);
        sameStarChar(input);

    }

    private static void everyOtherLetter(String input, boolean even) {
        int i =1;

        if(even) { //if we need even chars, start with 1;

            i=0;

        }

        for(; i < input.length(); i = i + 2){

            System.out.println(input.charAt(i));

        }
    }

    private static boolean sameStarChar(String input) {

        int i=0;

        for (;i < input.length();i=i+1){

            if (input.substring(i,(i+1))=="*"){


                if(input.substring((i-1),i)==input.substring((i+1),(i+2))){

                    return true;
                }

                else 

                    return false;
            }  
        }

    }
}
  • `if (input.substring(i,(i+1))=="*")`, `if(input.substring((i-1),i)==input.substring((i+1),(i+2)))` -> [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Sep 03 '17 at 23:29
  • Aside: consider `for (int i = even ? 0 : 1; i < input.length(); i += 2) {` It saves four lines of code. – Stephen C Sep 03 '17 at 23:50

1 Answers1

1

In this loop:

    for (;i < input.length();i=i+1){
        if (input.substring(i,(i+1))=="*"){
            if(input.substring((i-1),i)==input.substring((i+1),(i+2))){
                return true;
            }
            else 
                return false;
        }  
    }

(As pointed out in a comment, == is wrong; use equals().)

If your code finds a *, then it will check the characters before and after to see if they're equal. If they are, it will return true from the whole method; if not, the method will return false. I think this logic is wrong, but I don't completely understand the requirements.

But the reason for the error is: what happens if there is no * in the string? Or if the for loop never executes (because the input string is empty)? Then the code will complete the for loop without ever returning anything. Then what happens? The code has to return something. The compiler sees that if you get to this point, the method will return without a return statement telling it what value to return, which is a no-no (except for a void method). That's why you're getting the error. You may think that the input will never be empty and will always have * somewhere in it, but the compiler doesn't know that. So you'll need a return or throw somewhere after the loop.

I'm suspicious of your logic. Is it possible for there to be more than one * in the string? If so, then what is the method supposed to do--check only the first one, or check all of them? If the method needs to make sure that the characters around every * are the same, then the method cannot return true until it has checked all of them. This means that you cannot return true until the loop is completed. You're returning true from inside the loop, which will give you the wrong answer. I'm not certain of this, since I don't really know the specifics of what the method is supposed to accomplish. But this is a common mistake among newer programmers who use a loop to try to search for something or test all occurrences of something.

(One final note: if you are looking at individual characters, it's much easier to use charAt() instead of substring().)

ajb
  • 31,309
  • 3
  • 58
  • 84