Steph78
Steph78

Reputation: 5

How to count a specific character within a string using a while Loop - Java

I'm extremely new to this and while I was able to do this with a for loop, the assignment requires a while loop. I tried the below which is not working how it should. Please help!

package charcounter;

import java.util.Scanner;

public class CharCounter {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char userChar = '0';
        String inputEntry = "";
        String inputCharacter = "";
        int foundOccurrences = 0;

        System.out.print("Please enter one or more words: ");
        inputEntry = in.nextLine();

        System.out.print("\nPlease enter one character: ");
        inputCharacter = in.next();
        userChar = inputCharacter.charAt(0);

        while (foundOccurrences < inputEntry.length()) {
            if (userChar == inputEntry.charAt(0)) {

            }

            System.out.println("There is " + foundOccurrences + " occurrence(s) of " + inputCharacter + " in test.");

            foundOccurrences++;
        }

    }

}

Upvotes: 0

Views: 2984

Answers (4)

gati sahu
gati sahu

Reputation: 2641

One index to keep the loop counter for iterating through string and
foundOccurrences counter to keep the count if the given character found on the string.

Another approach to check from both side.

int start =0;
        int end=inputEntry.length()-1;
        while (start  < end) {
            if (userChar == inputEntry.charAt(start)) {
                foundOccurrences++;
            }
            if (userChar == inputEntry.charAt(end)) {
                foundOccurrences++;
            }
            start++;
            end--;
        }

BUG fix:

public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            char userChar = '0';
            String inputEntry = "";
            String inputCharacter = "";
            int foundOccurrences = 0;

            System.out.print("Please enter one or more words: ");
            inputEntry = in.nextLine();

            System.out.print("\nPlease enter one character: ");
            inputCharacter = in.next();
            userChar = inputCharacter.charAt(0);
            int index = 0;
            while (index < inputEntry.length()) {
                if (userChar == inputEntry.charAt(index)) {
                    foundOccurrences++;
                }
                index++;
            }
            System.out.println("There is " + foundOccurrences + " occurrence(s) of " + inputCharacter + " in test.");
        }

Upvotes: 0

Mihai Savin
Mihai Savin

Reputation: 61

You can use an index variable i that will help you go through the string's characters one by one, in order to check the equality. Something like:

while (i < inputEntry.length()) { ... i++; }

Upvotes: 0

urag
urag

Reputation: 1258

You really should try and debug your program and try to find a solution you profit a lot from this as a programmer.You have two problems in you code 1. You always test the same char in input text 2. Your foundOccurrences variable is not used like an occurrence counter instead it is incremented both if do or do not find a letter in the text here is a simple solution for you:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    char userChar = '0';
    String inputEntry = "";
    String inputCharacter = "";
    int foundOccurrences = 0;

    System.out.print("Please enter one or more words: ");
    inputEntry = in.nextLine();

    System.out.print("\nPlease enter one character: ");
    inputCharacter = in.next();
    userChar = inputCharacter.charAt(0);

    int index = 0;
    while (index < inputEntry.length()) {
        if (userChar == inputEntry.charAt(index)) {
            foundOccurrences++;
        }
        index++;
    }

    System.out.println("There is " + foundOccurrences + " occurrence(s) of " + inputCharacter + " in test.");
}

Upvotes: 1

khusrav
khusrav

Reputation: 5307

Something like this:

        int i = 0;
        while (i < inputEntry.length()) {
            if (userChar == inputEntry.charAt(i++)) {
                foundOccurrences++;
            }
        }
        System.out.println("There is " + foundOccurrences + " occurrence(s) of " + inputCharacter + " in test.");

fixed the bug

Upvotes: 2

Related Questions