2

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.

Arcee
  • 149
  • 8
  • Are you meaning to print out the value of a instead of the value of m? – Nick Acosta May 28 '17 at 00:34
  • 1
    One-liner using streams: `return tiles.chars().filter(c -> c == letter).count()`. – beatngu13 May 28 '17 at 00:47
  • @beat did you mean `Arrays.toStream(tiles.toCharArray()).filter(c -> c == letter).count()` – jmj May 28 '17 at 01:22
  • `tiles.indexOf(letter)` will output valid non negative index if `letter` is present in `titles`. in your code both are not changing during the loop execution. `abcdee` does contain `e` hence its printing like that – nits.kk May 28 '17 at 02:17
  • @JigarJoshi no, I mean [`CharSequence#chars()`](http://docs.oracle.com/javase/8/docs/api/java/lang/CharSequence.html#chars--), which directly gives you a stream of characters. – beatngu13 May 28 '17 at 08:40

3 Answers3

3

The easiest way to fix your code is to change the logic in your counting method to

    int a = 0;      
    for (char x : tiles.toCharArray()) {
        if (x == letter) {
            a += 1;
        }
    }
    return a;

There are better ways, though.

You might want to look as this old Stack Overflow question which is a more general solution to the problem you are working on.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • Thanks Ray I should have kept it simple... thanks for reminding me...you're a ninja.. :) – Arcee May 28 '17 at 00:45
1

The indexOf(String target) method searches left-to-right inside the given string for a "target" string. The indexOf() method returns the index number where the target string is first found or -1 if the target is not found. So if 'e' is present it would return true . You have not used variable x anywhere in your loop first of all .Maybe you can try ,

if (x == letter) {
  a+ = 1
}

instead of

if (tiles.indexOf(letter) != -1) {
  m = 1;
}
Snehal
  • 11
  • 2
0

It's repeatedly printing 1 because you're using indexOf method, that means as long as the letter e exists within the string tiles it will always print Letter count is 1.To solve the problem at hand simply change:

if (tiles.indexOf(letter) != -1)

to:

if (x == letter)

An even simpler solution to return the letter count as of java-8 is:

public int countLetter(char letter) {
       return (int)tiles.chars().filter(e -> e == letter).count();
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126