-4

I'm currently coding a program that takes in a string and a single character (if you decide to put a single one in that is) and (is supposed) to check how many times that character is in the string and at the end print out the amount of times it is in there. For some reason it isn't working and I would like some help; thank you!

import java.util.Scanner;

public class HowManyChars {
    public HowManyChars() {
        Scanner sc = new Scanner (System.in);
        String askPhrase;
        String askChar;
        int charCounter = 0;
        System.out.println("Enter a phrase");
        askPhrase = sc.nextLine();
        System.out.println("Enter a letter");
        askChar = sc.nextLine();

        for (int i = 0; i < askPhrase.length(); i++) {
            if (askPhrase.substring(i, i + 1) == askChar) {
                    charCounter = charCounter + 1;
            }
        }

        System.out.println("There are " + charCounter + " " + askChar + " in " + askPhrase);
    }
}
  • 1
    have you checked that your loop executes? Have you checked that your if statement returns true? – Stultuske Sep 27 '19 at 06:31
  • 3
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Mushif Ali Nawaz Sep 27 '19 at 06:34
  • Use `askPhrase.charAt(i) == askChar` where `askChar` is a `char` and not a `String`. – SedJ601 Sep 27 '19 at 06:35
  • @Sedrick If I were to go about this approach, how would I use a scanner to get user input for the character. .nextLine() doesn't work with chars. Thanks for the input though! –  Sep 27 '19 at 06:36
  • 1
    @MushifAliNawaz Thanks, I actually saw this before but couldn't really understand it with what I had going currently so I decided to ask a separate question. Thanks though! –  Sep 27 '19 at 06:37
  • `askChar.getCharAt(0)` should work. – SedJ601 Sep 27 '19 at 06:48
  • @Sedrick `.getCharAt(0)` is not a method in `String`. It is `.charAt(0)`. – Mushif Ali Nawaz Sep 27 '19 at 06:49
  • 1
    Sorry, that's what I meant. – SedJ601 Sep 27 '19 at 06:50
  • 1
    @Sedrick Yes, I got it. I have already told him about that in the comments of the answer. And it actually solved his problem. – Mushif Ali Nawaz Sep 27 '19 at 06:51

2 Answers2

1

I give credit for this answer to @Mushif, who correctly figured out the problem. Your current comparison logic is comparing a string against a character:

for (int i=0; i < askPhrase.length(); i++) {
    //        String                     char
    if (askPhrase.substring(i, i + 1) == askChar) {
        charCounter = charCounter + 1;
    }
}

Try iterating the character set of the input word and then compare apples to apples:

for (int i=0; i < askPhrase.length(); i++) {
    if (askPhrase.charAt(i) == askChar) {
        charCounter = charCounter + 1;
    }
}

You could also use an enhanced loop directly on the input's character set:

for (char chr : askPhrase.toCharArray()) {
    if (chr == askChar) {
        charCounter = charCounter + 1;
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • So even if askChar is labeled as a String, if it contains a single letter it is treated as a char? Also I have an error up here: "how would I use a scanner to get user input for the character. .nextLine() doesn't work with chars" –  Sep 27 '19 at 06:39
  • @CoreyMostero You could use: `.next().charAt(0)` – Mushif Ali Nawaz Sep 27 '19 at 06:39
  • @CoreyMostero I think what your code is actually doing is comparing a `String` with one character in it against a boxed `Character` object, also representing one character. But, if you check the `equals()` method being used to check if the two objects are equal, it will automatically fail, because they are from two different (unrelated) classes. As for your scanner question, that's another issue. – Tim Biegeleisen Sep 27 '19 at 06:40
  • @TimBiegeleisen `.substring()` returns a `String` and `askChar` is also a `String`. So he is basically comparing `String` == `String` that's why I have voted for a duplicate. – Mushif Ali Nawaz Sep 27 '19 at 06:43
-1

import java.util.Scanner;

public class HowManyChars {
    public HowManyChars() {
        Scanner sc = new Scanner (System.in);
        String askPhrase;
        char askChar;
        char[] askChars;
        int charCounter = 0;
        System.out.println("Enter a phrase");
        askPhrase = sc.nextLine();
        askChars = askPhrase.toCharArray();
        System.out.println("Enter a letter");
        askChar = sc.next().charAt(0);
        sc.close();
        for (int i = 0; i < askChars.length; i++) {
            if (Character.toLowerCase(askChar) == Character.toLowerCase(askChars[i])) {
                    charCounter ++;
            }
        }

        System.out.println("There are " + charCounter + " " + askChar + " in " + askPhrase);
    }
}
Saptarsi
  • 796
  • 5
  • 13