I need help making a word frequency counter that also gives percentages of the amount of time the character occured. I am having trouble compiling my program. Thanks.

import java.util.*;

public class GoodLetterCounter
 {
   public static void main(String[] args)
   {
      
     System.out.println("\n-This program will show how many times" +
                          " the character showed up in the string.-" );
                          
     
      System.out.println("");
     
     Scanner keyboard = new Scanner(System.in);
      
      int charCount = 0;
      
      System.out.print("Please enter a string: ");
      String userString = keyboard.nextLine();
      System.out.print("Please enter a character that was in the string: ");
      char userChar = keyboard.nextLine().charAt(0);
      
      
                     
      for(int i= 0;i<userString.length();i++)
      {
              if (userString.charAt(i) == userChar)
                  {
                  charCount++;
                  }
      }
      System.out.println("\nThe character " +"\"" + userChar +
                          "\" appeared " + charCount+ " times." );   
      
      System.out.println("the frequency was" + charCount/userString + "%");           
                
    }
}

Recommended Answers

All 11 Replies

Member Avatar for Dukane

The last line when you try to detemine the percentage, you are dividing a character by a String. You can't do that!

Member Avatar for Dukane

You'll also notice that once you fix the bottom line, that you are doing integer division, so the result will be an integer -- either 0 or 1. You need to do floating point division in order to get a percentage. And you also forgot to multiply by 100.

Other than that, I was able to get the program working just fine.

The last line when you try to detemine the percentage, you are dividing a character by a String. You can't do that!

Is there a way that I can type cast it?

I found a way to fix my previous problem. But now I want to limit the decimal place to 2 places and make a variable for frequency.
Also instead of typing in one character to see its frequency, I want to see all letters occurence and frequencies and the program should stop when the user enters something other than a letter (no white space). Some how I keep getting compiler errors.
Thanks

import java.util.*;
import java.text.*;

public class GoodLetterCounter
 {
   public static void main(String[] args)
   {
      
     System.out.println("\n'This program will return how many times" +
                          " the character you entered showed up in" +
                          " the string you entered'.");
     
      System.out.println("");
     
     Scanner keyboard = new Scanner(System.in);
      
      int charCount = 0;
      String userString = new String();
      float frequency = (charCount * 100/(userString.length()));
      
      do
        {
          
          
          System.out.print("Please enter a string: ");
          userString = keyboard.nextLine();
          
      
          System.out.print("Now enter a character from the string: ");
          char userChar = keyboard.nextLine().charAt(0);
         
          
                         
          for(int i= 0; i<userString.length();i++)
              {
                  if (userString.charAt(i) == userChar)
                      {
                      charCount++;
                      }
              }
          System.out.println("\nThe character " +"\"" + userChar +
                              "\" appeared " + charCount+ " times." );   
          
          System.out.println("The frequency was " + frequency + "%");           
          
          System.out.println(""); 
                              
         } while (userString.length() != 0);
    }
        
}

Sorry for the double post. I don't see a delete option.

Member Avatar for Dukane

WHAT are the messages you are getting? Chances are it's telling you what the problem is!

WHAT are the messages you are getting? Chances are it's telling you what the problem is!

The compiler isn't giving me any errors now. But when I execute the program it says Exception in thread "main" java.lang.ArithmeticException: /by zero

Member Avatar for Dukane

Alright, so fix that problem and you should be good to go!

Alright, so fix that problem and you should be good to go!

I fixed it now, but its not so pretty. Plus I want to see all letters occurence and frequencies. I also the program should stop when the user enters something other than a letter (no white space) or "quit".

import java.util.*;
import java.text.*;

public class GoodLetterCounter
 {
   public static void main(String[] args)
   {
      
     System.out.println("\n'This program will return how many times" +
                          " the character you entered showed up in" +
                          " the string you entered'.");
     
      System.out.println("");
     
     //needed for scanner class
      Scanner keyboard = new Scanner(System.in);
      
      double charCount = 0;
      String userString = new String();
            
      do
        {
          
          // get users string
          System.out.print("Please enter a string: ");
          userString = keyboard.nextLine();
          
          // get users character
          System.out.print("Now enter a character from the string: ");
          char userChar = keyboard.nextLine().charAt(0);
          // tell the user what the program does
          
                         
          for(int i= 0; i<userString.length();i++)
              {
                  if (userString.charAt(i) == userChar)
                      {
                      charCount++;
                      }
              }
          System.out.println("\nThe character " +"\"" + userChar +
                              "\" appeared " + charCount+ " times." );   
          
          System.out.println("The frequency was " 
                              + charCount * 100/(userString.length()) + "%");           
          
          System.out.println(""); 
                              
         } while (userString.length() != 0);
    }
        
}

I also need the char output to be upper case.

Now :

a:
5 times 5 times
25% frequency

What I need:

A:
5 times
25% frequency

I did the main thing, but now I want the program to end when the user enters a space or an invalid character. Is there anyway for me to stop at two decimal places? Any suggestions?

import java.util.*; // For Standard Input Scanner class
import java.text.*; // For decimal format DecimalFormat class

 
public class count {
 
     // Constants
    private final int MAX = 27;
    private int [] lower = new int [MAX];
 
    
    
    public static void main(String[] args) {
        
        // initialzes scanner and private instance variables/object
        Scanner keyboard = new Scanner(System.in);
 
        char current;
        final int MAX = 27;
        int [] lower = new int [MAX];
         int Spaces = 0;
 
 
    System.out.println("Enter a sentence:");
    String line = keyboard.nextLine (); 
    
    //converts the string into an int
    line = line.toLowerCase();
     
 
    //sets requirements for the array and if/else statements
    for (int ch = 0; ch < line.length(); ch++) 
     
        { 
        current = line.charAt(ch); 
            
            if (current >= 'a' && current <= 'z') 
                lower[current -'a']++; 
            else if (current == ' ') 
                Spaces ++; 
         } 
     
    System.out.println();
 
     
     //sets the requirement for the 2nd array and prints occurence
     //and the frequency (percent) 
    for (int letter = 0; letter < lower.length;letter++) 
    { 
         
    if (lower[letter]>0)
        {
 
     
        System.out.print((char)(letter + 'a')); 
        System.out.println();
        
        System.out.println("    count = " + lower[letter] );
        System.out.println("    percentage = " + 
                            (float)lower[letter] * 100/(line.length()) + "%");
        System.out.println(); 
        }
 
    } 
    
    //counts how many spaces
    System.out.println (); 
    System.out.println ("But had " + Spaces + " spaces: "); 
 
    
    }
}
Member Avatar for Dukane

Keep in mind that a and A are two different chracters to the computer!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.