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:
String str = "71 geese - 83 cars - 58 cows- 64 mooses";
String numberArray[] = str.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);
}
to find out the count of each number word in the Map:
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.