Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: COUNTING THE FREQUENCY OF NUMBERS INSIDE A TEXT FILE

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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).

    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. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: COUNTING THE FREQUENCY OF NUMBERS INSIDE A TEXT FILE

    The numbers inside that text file would be considered as strings, am I right?
    it can be read as characters also, but in other class like scanner you can read it as int or double.

    Would using "Double.parseDouble ()" help?
    quiet tricky or you might end up confused with that way I think.

    here is one way using BufferedReader:
    1st - create a string. read each character in the file, once you encounter a digit, equate the string to that character
    2nd - then from that character, read the next characters and check if it is a number, if so concatenate it to the string you declared above, and once you read a non digit character, there you go the parsing part and then return to 1st step.

  3. The Following User Says Thank You to dicdic For This Useful Post:

    GregBrannon (April 7th, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: COUNTING THE FREQUENCY OF NUMBERS INSIDE A TEXT FILE

    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 ();
    		}
    	}
    }
    Last edited by PC_flea; April 8th, 2014 at 07:17 AM. Reason: no code tags around included sample code

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: COUNTING THE FREQUENCY OF NUMBERS INSIDE A TEXT FILE

    Also posted at: Counting the frequency of numbers inside a text file
    and
    http://forums.codeguru.com/showthrea...de-a-text-file
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading text file and counting the number of words, integers, and floats.
    By Jsmooth in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: April 12th, 2012, 06:39 PM
  2. reading from text file produces extra s p a c e s, even on numbers 1 0 1
    By Spidey1980 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 3rd, 2011, 09:18 PM
  3. How to generate 13 digit numbers to text file
    By duanedtp in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 11:30 AM
  4. counting words of a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 6th, 2010, 03:40 PM
  5. Counting Vowels and Prepositions in a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 27th, 2010, 07:36 PM