0

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

    }

}
3
  • You are comparing the first character only. Commented Jun 27, 2017 at 12:24
  • Your loop condition seems off. You are always incrementing the number of times the specified character is found. You will want to create two variables: one to loop with, and another to count the number of times the specified character occurs. Commented Jun 27, 2017 at 12:25
  • Post the code of your solution using for-loop, so we can point you the right direction. Commented Jun 27, 2017 at 12:26

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, do you know how I would add a test that user only entered one character?
1

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

Comments

0

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

Comments

0

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

1 Comment

Thanks, do you know how I would add a test that user only entered one character?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.