| | |
Word Frequency Counter Help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2006
Posts: 7
Reputation:
Solved Threads: 0
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)
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 + "%"); } }
Last edited by the jaguar; Dec 6th, 2006 at 3:59 pm.
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.
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
•
•
Join Date: Dec 2006
Posts: 7
Reputation:
Solved Threads: 0
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
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)
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); } }
Last edited by the jaguar; Dec 6th, 2006 at 5:47 pm. Reason: deleting something.
•
•
Join Date: Dec 2006
Posts: 7
Reputation:
Solved Threads: 0
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)
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); } }
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Question on arrays & methods
- Next Thread: about -xms option
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application array arrays automation binary bluetooth button character chat class classes client code component css csv database draw eclipse ee error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaprojects jni jpanel julia jvm key linked linux list loan loop map method methods mobile netbeans newbie objects oracle oriented output panel phone print problem program programming project projects recursion replaydirector reporting researchinmotion robot rotatetext scanner screen se server service set size sms software sort sql string swing test threads time transfer tree ubuntu windows





