What's new

Java Character Counting Application

Chris7S

Nerd by definition, programmer by trade
Messages
1,522
Reaction score
490
So I was approached by another student the other day that is taking a Java course and he asked how to count the occurrences of a specific character in a string. Since I have already taken a few Java courses and taught myself most of the language I decided to help him out and write him a snippet of code. Needless to say, around 5 minutes later I came up with the solution for him. This sparked an idea that anytime a fellow student comes to me for help I should release it here on Se7ensins because I noticed some of the developers here are starting college and they will need to know some of this basic knowledge. So here is my first of hopefully many releases that point out key concepts in Java and other languages. I hope this helps someone out in the log run because I know small applications like this really helped me when learning how to code. Enjoy!

Code:
package character.count;
import java.util.*;

/**
 *
 * @author Chris
 */

public class CharacterCount 
{
     public static void main(String[] args)
     {
          int count = 0;
          int i = 0;
   
          Scanner userInputScanner = new Scanner(System.in);
          System.out.println("What string would you like to use?");
          String userInput = userInputScanner.nextLine();
          System.out.println("What character would you like to search for?");
          char character = userInputScanner.next().charAt(0);
   
          for (i=0; i < userInput.length(); i++)
         {
              if (userInput.charAt(i) == character)
             {
                  count++;
             }
        }
       System.out.println("The character " + character + " appears " + count + " times in the string!");
     }
}
Output

Again, I hope you enjoyed this and it helped you learn more about for loops and some of the uses of the java.util class. Also, I did not check for case in this source code so it returns only the number of results where the character is in the same case of the string. This can easily be achieved by using equalsIngoreCase(string)
 

DarkPulseModz

Newbie
Messages
21
Reaction score
5
Python:
sentence ='This is a(n) example of counting Occurrences in Python'
sentence.count('c')
print("Number Occur(s):" + count + "Times.")

Result:
Code:
Number Occur(s): 4 Times.
 

MatthewH

Member
Messages
1,612
Reaction score
628
What's funny is, a while ago we had to do this with an Arduino. However, instead of working with one string we were working with an array.
It proved to be difficult because of how it was processing everything in we had to like make all sorts of work-arounds for it.

You should have it do an array of strings now :wink:

Good job though. :tongue:
 

YYes

Enthusiast
Messages
177
Reaction score
52
Code:
charCount :: Char -> String -> Int
charCount ch (c:cs) = (
    if ch == c
    then 1
    else 0
    ) + charCount ch cs
charCount _ _ = 0
Code:
> charCount 'o' "The quick brown fox jumps over the lazy dog."
==> 4
Python:
sentence ='This is a(n) example of counting Occurrences in Python'
sentence.count('c')
print("Number Occur(s):" + count + "Times.")

Result:
Code:
Number Occur(s): 4 Times.
Pretty sure count isn't scoped here.
 
Top Bottom