0

I have a source code here that counts the frequency of alphabetic characters and non-alphabetic characters (see the source code below).

        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?

2
Contributors
1
Reply
8
Views
3 Years
Discussion Span
Last Post by albucurus
1

Hi,
Read the content of the file in a String

String text = "";
String line = "";
while ((line = in.readLine()) != null) 
{
        text += line;
}

After that split the string into array, and display first 2 character of each element:

for (String retval: text.split("-"))
{
         System.out.println(retval.substring(0,1));
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.