2uzhan.com
Advertisement
Now Place:2uzhan.com » COUNTING THE FREQUENCY OF NUMBERS INSIDE A TEXT FILE

COUNTING THE FREQUENCY OF NUMBERS INSIDE A TEXT FILE

Software Development @ April 7, 2014   Views: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?

--------------Solutions-------------

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));
}
Window x:Class="HexagonWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:HexagonWindow"
        mc:Ignorable="d"
        Title="Hexa" Height="300" Width="300" WindowStyle="None"
        AllowsTransparency="True" Background="Transparent" MouseLeftButtonDown="Window_MouseLeftButtonDown">
    <Grid>
        <!--Points="100,0 200,0 300,100 300,200 200,300 100,300 0,200 0,100" use these points for octagon window--> ...

Before I get into the specific issues, I want to provide some other details:

  • Normally, I work on a live, paid server (even in dev); I realize that is ...

Hey there,

I am trying to create a register code. So basicly im making a quiz and every time a user starts up my program he/she needs to register (making ...

Tags:java
© 2018 2uzhan.com Contact