944,111 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5769
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 6th, 2006
0

Word Frequency Counter Help

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the jaguar is offline Offline
7 posts
since Dec 2006
Dec 6th, 2006
0

Re: Word Frequency Counter Help

The last line when you try to detemine the percentage, you are dividing a character by a String. You can't do that!
Reputation Points: 45
Solved Threads: 28
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006
Dec 6th, 2006
0

Re: Word Frequency Counter Help

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
Reputation Points: 45
Solved Threads: 28
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006
Dec 6th, 2006
0

Re: Word Frequency Counter Help

Click to Expand / Collapse  Quote originally posted by Dukane ...
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the jaguar is offline Offline
7 posts
since Dec 2006
Dec 6th, 2006
0

Re: Word Frequency Counter Help

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

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the jaguar is offline Offline
7 posts
since Dec 2006
Dec 6th, 2006
0

Re: Word Frequency Counter Help

Sorry for the double post. I don't see a delete option.
Last edited by the jaguar; Dec 6th, 2006 at 5:46 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the jaguar is offline Offline
7 posts
since Dec 2006
Dec 6th, 2006
0

Re: Word Frequency Counter Help

WHAT are the messages you are getting? Chances are it's telling you what the problem is!
Reputation Points: 45
Solved Threads: 28
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006
Dec 6th, 2006
0

Re: Word Frequency Counter Help

Click to Expand / Collapse  Quote originally posted by Dukane ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the jaguar is offline Offline
7 posts
since Dec 2006
Dec 6th, 2006
0

Re: Word Frequency Counter Help

Alright, so fix that problem and you should be good to go!
Reputation Points: 45
Solved Threads: 28
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006
Dec 6th, 2006
0

Re: Word Frequency Counter Help

Click to Expand / Collapse  Quote originally posted by Dukane ...
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".

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the jaguar is offline Offline
7 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Question on arrays & methods
Next Thread in Java Forum Timeline: about -xms option





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC