简体   繁体   中英

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++;
        }

    }

}

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

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.");
}

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++; }

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.");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM