Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a source code here that counts the frequency of alphabetic characters and non-alphabetic characters (see the source code below).

Java
import java.io.*;

public class letterfrequency {
	public static void main (String [] args) throws IOException {
		File file1 = new File ("letternumberfrequency.txt");
		BufferedReader in = new BufferedReader (new FileReader (file1));
		
		int nextChar;
		int other = 0;
		char ch;
		
		int [] count = new int [26];
		
		while ((nextChar = in.read())!= -1) {
			ch = ((char)nextChar);
			ch = Character.toLowerCase (ch);
			if (ch >= 'a' && ch <= 'z')
			  count [ch- 'a']++;
			else
			  other ++;
		}
		
		for (int i=0; i<26; i++){
			System.out.printf ("%c = %d \n", i+ 'A', count [i]);
		}
		
		System.out.println ("Non-alphabetic characters: " + other);
		in.close ();
	}
}


But, let's just say that now I have the following characters in the text file, "letternumberfrequency.txt":
71 geese - 83 cars - 58 cows- 64 mooses- 100 ants- 69 bangles- 90 molehills - 87 noses

The numbers inside that text file would be considered as strings, am I right?
But I want to extract the numbers so that I can also be able to count their frequency - not as individual digits but as whole numbers (that is how many "71", "83", "58", "64", etc. are there...).
Would using "Double.parseDouble ()" help?
Posted
Comments
Peter Leow 7-Apr-14 21:44pm    
You mean: there could be two 71's, four 58's, something like that?
code_flea 7-Apr-14 21:57pm    
Yes, that would be the case... Is using "Double.parseDouble()" then a good idea?
[no name] 7-Apr-14 21:49pm    
You would need to split your strings. Yes Double.parseDouble would certainly help. If you actually need to convert your integers to a double but why? If you are just counting occurrences of a particular string why is it that you would need to convert the string to a number at all?
code_flea 7-Apr-14 21:58pm    
So, are you implying that "charAt" is already a good enough solution to count the number of occurrences of how many 71s and 83s are in that text file? Or if charAt isn't a good idea, what do you have in mind?
[no name] 7-Apr-14 22:12pm    
I am not implying anything. I am straight saying that your premise is wrong. Your whole idea is to compare the string 71 (for example) with another string 71 that you read out of your file. Why do you need to convert the string to anything just to increase some counter variable?

1. First, split the whole string into array of number strings using non-digit as delimiters.
2. Next, count the array for repeated number strings and store them as key value pairs in a Map
3. Do whatever you like with the Map
See example:
C#
String str = "71 geese - 83 cars - 58 cows- 64 mooses";
// use regex to split by any non-digit delimiters
String numberArray[] = str.split("[^\\d]+");
// count and store the number word and its count in a Map
Map<String, Integer> map = new HashMap<String, Integer>();
for ( String numberWord : numberArray) {
   Integer counter = map.get(numberWord);
   if ( counter == null ) {
            counter = 0;
   }
   map.put(numberWord, counter + 1);
}

to find out the count of each number word in the Map:
C#
for ( String numberWord : map.keySet() ) {
  System.out.println("%s => %i", numberWord, map.get(numberWord));
}

I have not tested it for any bugs, just from my head, but the idea is there, good luck.

++++++++++++++[Added upon request]++++++++++++++++
My observation:
1. Typo and syntax errors, e.g. String, Integer, remember Java is case sensitive;
2. Missing imports for map etc.
3. logical error, why close the bufferedreader in the while loop?
4. Some other minor mistakes
See the following working example and compare it with yours:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Letternumberfrequency {
	public static void main(String[] args) throws IOException {
		
	    FileReader fr = new FileReader("letternumberfrequency.txt");
	    BufferedReader in = new BufferedReader(fr);
	
		String line;
		while ((line = in.readLine()) != null) {
		 
			String numberArray[] = line.split("[^\\d]+");
			Map<String, Integer> map = new HashMap<String, Integer>();
			for (String numberWord : numberArray) {
				Integer counter = map.get(numberWord);
				if (counter == null) {
					counter = 0;
				}
				map.put(numberWord, counter + 1);
			}
			
			for (String numberWord : map.keySet()) {
				System.out.println(numberWord + " => " +  map.get(numberWord));
			}

		}
		in.close();
	}
}

End of Tutorial.
 
Share this answer
 
v3
Comments
code_flea 8-Apr-14 8:25am    
If I am to insert your ideas in the code, would it look like the one below?

import java.io.*;

public class letternumberfrequency {
public static void main (String [] args) throws IOException {
File file1 = new File ("letternumberfrequency.txt");
BufferedReader in = new BufferedReader (new FileReader (file1));

int nextStr;
String str;

while ((nextStr = in.read())!= -1) {
String numberArray []= str.split ("[^\\d]+");
Map <string,integer> map = new HashMap <str,nextstr>();
for (String numberWord:numberArray){
integercounter = map.get (numberWord);
if (counter == null) {
counter = 0;
}
map.put (numberWord, counter +1);
}
for (String numbeWord:map.KeySet()){
System.out.println ("%s => %i", numberWord, map.get(numberWord));
}
in.close ();
}
}
}
Peter Leow 8-Apr-14 9:47am    
See addition in my solution.
Peter Leow 9-Apr-14 20:54pm    
Have you forgotten to accept this solution?
A similar question was answered at Count number of words in text file[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900