I have this code below, my goal is to count how many letter 'e' is in the String "abcdee"
.
class Sample1 {
String tiles;
public Sample1 (String tiles) {
this.tiles = tiles;
}
public int countLetter(char letter) {
int a = 0;
for (char x : tiles.toCharArray()) {
int m = 0;
if (tiles.indexOf(letter) != -1) {
m = 1;
}
a += m;
System.out.println("Letter count is " + m);
}
return a;
}
}
public class Sample {
public static void main(String[] args) {
Sample1 s = new Sample1("abcdee");
s.countLetter('e');
}
}
I expect that the code would give me this result:
Letter count is 0
Letter count is 0
Letter count is 0
Letter count is 0
Letter count is 1
Letter count is 1
and then maybe add all the 1's to get 2. But all I get is this when I run it:
Letter count is 1
Letter count is 1
Letter count is 1
Letter count is 1
Letter count is 1
Letter count is 1
Hope you can help me out.