Word Frequency Counter Help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2006
Posts: 7
Reputation: the jaguar is an unknown quantity at this point 
Solved Threads: 0
the jaguar the jaguar is offline Offline
Newbie Poster

Word Frequency Counter Help

 
0
  #1
Dec 6th, 2006
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.

  1. import java.util.*;
  2.  
  3. public class GoodLetterCounter
  4. {
  5. public static void main(String[] args)
  6. {
  7.  
  8. System.out.println("\n-This program will show how many times" +
  9. " the character showed up in the string.-" );
  10.  
  11.  
  12. System.out.println("");
  13.  
  14. Scanner keyboard = new Scanner(System.in);
  15.  
  16. int charCount = 0;
  17.  
  18. System.out.print("Please enter a string: ");
  19. String userString = keyboard.nextLine();
  20. System.out.print("Please enter a character that was in the string: ");
  21. char userChar = keyboard.nextLine().charAt(0);
  22.  
  23.  
  24.  
  25. for(int i= 0;i<userString.length();i++)
  26. {
  27. if (userString.charAt(i) == userChar)
  28. {
  29. charCount++;
  30. }
  31. }
  32. System.out.println("\nThe character " +"\"" + userChar +
  33. "\" appeared " + charCount+ " times." );
  34.  
  35. System.out.println("the frequency was" + charCount/userString + "%");
  36.  
  37. }
  38. }
Last edited by the jaguar; Dec 6th, 2006 at 3:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 263
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

Re: Word Frequency Counter Help

 
0
  #2
Dec 6th, 2006
The last line when you try to detemine the percentage, you are dividing a character by a String. You can't do that!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 263
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

Re: Word Frequency Counter Help

 
0
  #3
Dec 6th, 2006
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.
Last edited by Dukane; Dec 6th, 2006 at 4:00 pm. Reason: added to it
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 7
Reputation: the jaguar is an unknown quantity at this point 
Solved Threads: 0
the jaguar the jaguar is offline Offline
Newbie Poster

Re: Word Frequency Counter Help

 
0
  #4
Dec 6th, 2006
Originally Posted by Dukane View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 7
Reputation: the jaguar is an unknown quantity at this point 
Solved Threads: 0
the jaguar the jaguar is offline Offline
Newbie Poster

Re: Word Frequency Counter Help

 
0
  #5
Dec 6th, 2006
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

  1. import java.util.*;
  2. import java.text.*;
  3.  
  4. public class GoodLetterCounter
  5. {
  6. public static void main(String[] args)
  7. {
  8.  
  9. System.out.println("\n'This program will return how many times" +
  10. " the character you entered showed up in" +
  11. " the string you entered'.");
  12.  
  13. System.out.println("");
  14.  
  15. Scanner keyboard = new Scanner(System.in);
  16.  
  17. int charCount = 0;
  18. String userString = new String();
  19. float frequency = (charCount * 100/(userString.length()));
  20.  
  21. do
  22. {
  23.  
  24.  
  25. System.out.print("Please enter a string: ");
  26. userString = keyboard.nextLine();
  27.  
  28.  
  29. System.out.print("Now enter a character from the string: ");
  30. char userChar = keyboard.nextLine().charAt(0);
  31.  
  32.  
  33.  
  34. for(int i= 0; i<userString.length();i++)
  35. {
  36. if (userString.charAt(i) == userChar)
  37. {
  38. charCount++;
  39. }
  40. }
  41. System.out.println("\nThe character " +"\"" + userChar +
  42. "\" appeared " + charCount+ " times." );
  43.  
  44. System.out.println("The frequency was " + frequency + "%");
  45.  
  46. System.out.println("");
  47.  
  48. } while (userString.length() != 0);
  49. }
  50.  
  51. }
Last edited by the jaguar; Dec 6th, 2006 at 5:47 pm. Reason: deleting something.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 7
Reputation: the jaguar is an unknown quantity at this point 
Solved Threads: 0
the jaguar the jaguar is offline Offline
Newbie Poster

Re: Word Frequency Counter Help

 
0
  #6
Dec 6th, 2006
Sorry for the double post. I don't see a delete option.
Last edited by the jaguar; Dec 6th, 2006 at 5:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 263
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

Re: Word Frequency Counter Help

 
0
  #7
Dec 6th, 2006
WHAT are the messages you are getting? Chances are it's telling you what the problem is!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 7
Reputation: the jaguar is an unknown quantity at this point 
Solved Threads: 0
the jaguar the jaguar is offline Offline
Newbie Poster

Re: Word Frequency Counter Help

 
0
  #8
Dec 6th, 2006
Originally Posted by Dukane View Post
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 263
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

Re: Word Frequency Counter Help

 
0
  #9
Dec 6th, 2006
Alright, so fix that problem and you should be good to go!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 7
Reputation: the jaguar is an unknown quantity at this point 
Solved Threads: 0
the jaguar the jaguar is offline Offline
Newbie Poster

Re: Word Frequency Counter Help

 
0
  #10
Dec 6th, 2006
Originally Posted by Dukane View Post
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".

  1. import java.util.*;
  2. import java.text.*;
  3.  
  4. public class GoodLetterCounter
  5. {
  6. public static void main(String[] args)
  7. {
  8.  
  9. System.out.println("\n'This program will return how many times" +
  10. " the character you entered showed up in" +
  11. " the string you entered'.");
  12.  
  13. System.out.println("");
  14.  
  15. //needed for scanner class
  16. Scanner keyboard = new Scanner(System.in);
  17.  
  18. double charCount = 0;
  19. String userString = new String();
  20.  
  21. do
  22. {
  23.  
  24. // get users string
  25. System.out.print("Please enter a string: ");
  26. userString = keyboard.nextLine();
  27.  
  28. // get users character
  29. System.out.print("Now enter a character from the string: ");
  30. char userChar = keyboard.nextLine().charAt(0);
  31. // tell the user what the program does
  32.  
  33.  
  34. for(int i= 0; i<userString.length();i++)
  35. {
  36. if (userString.charAt(i) == userChar)
  37. {
  38. charCount++;
  39. }
  40. }
  41. System.out.println("\nThe character " +"\"" + userChar +
  42. "\" appeared " + charCount+ " times." );
  43.  
  44. System.out.println("The frequency was "
  45. + charCount * 100/(userString.length()) + "%");
  46.  
  47. System.out.println("");
  48.  
  49. } while (userString.length() != 0);
  50. }
  51.  
  52. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC