0

My task is to create function which returns true if string parameter contains 1,2 or 3 "e" characters, for loop is needed. It looks like equals() method passes everything in and increases counter whenever sLetter = "e" or not. Or maybe problem is somewhere else?

static boolean checkLetter (String paramOne){
    int count = 0;
    for (int i = 0; i <= paramOne.length() - 1; i++) {
        char letter = paramOne.charAt(i);
        String sLetter = Character.toString(letter);

        if (sLetter.equals("e"));
        {
            count++;
        }
    }

    System.out.print((count >= 1) && (count <= 3));
    return (count >= 1) && (count <= 3);
}
Sneftel
  • 40,271
  • 12
  • 71
  • 104
Aligator
  • 13
  • 1
  • This article can help you: https://stackoverflow.com/questions/767372/string-equals-versus – mad4n7 Sep 26 '18 at 20:41
  • What problem? Also `letter == 'e'` and `if (sLetter.equals("e"));` has a really bad typo. Remove the semicolon. – Elliott Frisch Sep 26 '18 at 20:41
  • 1
    Code looks correct, but you could just check to see if letter == 'e' instead of converting to a string. – JustinKSU Sep 26 '18 at 20:42
  • 7
    Typo - you've got a semi-colon at the end of your `if` statement. – Makoto Sep 26 '18 at 20:42
  • And where are some crucial informations like eg INPUT and actual OUTPUT ?? – Antoniossss Sep 26 '18 at 20:44
  • 1
    If you use a good IDE (e.g. Eclipse), it would give you this warning at the end of the `if` statement: **Empty control-flow statement** – Andreas Sep 26 '18 at 20:44
  • More precisely, your if statement is followed by **an empty statement** (a single semicolon). Everything within the curly brackets are just part of a normal **code block**. – MC Emperor Sep 26 '18 at 20:47

2 Answers2

-1

Remove the semicolon from the end of the if statement.

static boolean checkLetter (String paramOne){
    int count = 0;
    for (int i = 0; i <= paramOne.length() - 1; i++) {
        char letter = paramOne.charAt(i);
        String sLetter = Character.toString(letter);

        if (sLetter.equals("e"))
        {
            count++;
        }
    }

    System.out.print((count >= 1) && (count <= 3));
    return (count >= 1) && (count <= 3);
}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
-1

You have an extra semicolon ";" on the if statement, so nothing happens if it's true and the count always goes up

change

 if (sLetter.equals("e"));
 {
   count++;
 }

to

 if (sLetter.equals("e")) {
   count++;
 }

Also you can simplify the logic by doing

if (letter == 'e') {
   count++;
}
JustinKSU
  • 4,875
  • 2
  • 29
  • 51
  • 3
    Vote to close. Don't answer questions this simple. – Makoto Sep 26 '18 at 20:45
  • its in the comments already and for closing. – Antoniossss Sep 26 '18 at 20:45
  • @Makoto Why? This is not obvious to a new developer. Shouldn't we be helping folks like this instead of ignoring new users? – JustinKSU Sep 26 '18 at 20:48
  • 1
    It's a typo which won't be of any value to anyone else in any meaningful way considering how much Google-able material already exists on it. – Makoto Sep 26 '18 at 20:49
  • Maybe it should be closed as duplicate of https://stackoverflow.com/questions/14112515/semicolon-at-end-of-if-statement instead of saying it's off topic – JustinKSU Sep 26 '18 at 20:51