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: Reading characters from a simple output to list words.

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading characters from a simple output to list words.

    Hello there,
    It's been a little while since I used Java and I'm trying to brush up on my skills, I have the following problem.

    I have a string in the SimpleCharacterReader class which is like the following:

     "Word word, wOrd word word worD\n" +
     "a few more words, wordS, yes more words\n" +

    SimpleCharacterReader has a method called getNextChar() which does what you'd expect, returns the next character in the sequence. What I am trying to do is create a list of word frequency so for the above

    word - 6
    words - 3
    etc etc

    Now currently I have the string being read as follows:

            CharacterReader characterReader = new SimpleCharacterReader();
     
            StringBuilder string = new StringBuilder();
            try{
                while(true){
                string.append(characterReader.getNextChar());
                }
            } catch(EOFException e) {
                try{characterReader.close();} catch(IOException ignore){}
            }
            Scanner sc = new Scanner(string.toString());

    The issue I have with this, is while it works for my little example, I want it to be able to count the words as it reads them, rather than creating a full string then counting. Are there any clean ways of doing this or do I have to just brute force it, add the characters until a delimiter is detected then calling that a word and putting it in the table?

    Sorry if that sounds a bit silly.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Reading characters from a simple output to list words.

    I would suggest the iterator class, using the hasNext and next methods of the class.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Reading characters from a simple output to list words.

    ....and if you need more assistance than that, provide more details on your project.
    The origin of the string (or data) you plan to work on. Perhaps one of the collections would be a better way to work on your data than a string, perhaps not.
    Do you just need to count the number of times each word appears?
    Your example seems to show a non-case sensetive setup, remember java is case sensetive.
    Do you just need to output the results as in on screen, or save them for later use?
    How many times will you have to work on the same data?
    Any other factor(s) which may change a best approach to this project...

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Reading characters from a simple output to list words.

    ...oh one last thing I would like to mention, one of the java collections, a map type, has the natural ability to save each key once and only once. With your data in such a map, you could ask if the key (each word in your project) already exists, and if so, ++ the value stored next to the key. On the other hand, if not, add it, and start the value behind the key (in your project, the number of times the word appears) to 1;

Similar Threads

  1. Replies: 12
    Last Post: March 17th, 2013, 01:38 AM
  2. Might be a thread leak problem reading a file into an Array list.
    By rushtonjj in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 10th, 2012, 06:56 AM
  3. 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
  4. Reading file of floats into array list and sorting into subsets
    By Skave in forum Collections and Generics
    Replies: 2
    Last Post: November 9th, 2011, 06:03 PM
  5. Reading ant output when exec the command
    By cipm66 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 23rd, 2010, 01:48 PM