public static void total() {

        Scanner s = new Scanner("five six seven eight");
        int num= 0;

        for(int i=0; i < s.length; i++)
        {
        if(s.charAt(i).equals("a") || s.charAt(i).equals("b")
        num++;
        }
        System.out.print(num);
    }
public static void main(String[] args) {
    total();

            }

What am I doing wrong ?

Recommended Answers

All 3 Replies

Can you explain what the code does and why you think there is something wrong?
Post the code's output and explain what is wrong with it.

Depends on the approach you are taking. Maybe give us a quick algorithm, then we can suggest improvements or a different option.

Your code will give a result of 0, because the letters a nor b are present in the given string. If you want to see if a particular character is a letter look at the IsLetter function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.