Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Counting the frequency of numbers inside a text file

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

    Code:
    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 ();
    	}
    }
    For now, the aforementioned source code only specifically counts alphabetic characters and lumps all the non-alphabet ones into a single counter value.
    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 frequency - not as individual digits but as whole numbers (that is, "71", "83", "58", "64", etc.).

    Would using "Double.parseDouble ()" help?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I think the easiest way would be to use the scanner, something like this:
    PHP Code:
            int other 0;
            
    int nums 0;
            
    char ch;
            
    int[] count = new int[26];
            
    Scanner s = new Scanner(in);
            while (
    s.hasNext())
            {
                if (
    s.hasNextInt())
                {
                    
    s.nextInt();
                    ++
    nums;
                }
                else
                {
                    
    ch s.findInLine(".").charAt(0);
                    if (
    ch >= 'a' && ch <= 'z')
                    {
                        ++
    count[ch 'a'];
                    }
                    else
                    {
                        
    System.out.println(ch " is an other");
                        ++
    other;
                    }
                }
            }
            
    s.close(); 
    Untested, works out okay in my head.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,806
    Thanks
    41
    Thanked 199 Times in 198 Posts
    there are probably some wicked regexes that would have this in a few lines... but I'm horrible at them :P

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •