Results 1 to 6 of 6
  1. #1
    chdn202002 is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default Read text file and count words.

    Hello

    I'm trying to write a program that inputs a text file and counts the words.

    Unfortunately, my output isn't counting but displaying only the words with out counting.

    My txt input looks like:

    Hello I like pizza and java programing.
    Sometimes I eat pizza and for breakfast and pizza for lunch
    Hello I like pizza and java programing.
    Sometimes I eat pizza and for breakfast and pizza for lunch
    Hello I like pizza and java programing.
    Sometimes I eat pizza and for breakfast and pizza for lunch
    Hello I like pizza and java programing.
    Sometimes I eat pizza and for breakfast and pizza for lunc

    My output looks like:1 and
    1 hello
    1 java
    1 like
    1 pizza
    1 programing
    2 and
    1 breakfast
    1 eat
    2 for
    1 lunch
    2 pizza
    1 sometimes
    1 and
    1 hello
    1 java
    1 like
    1 pizza
    1 programing
    2 and
    1 breakfast
    1 eat
    2 for
    1 lunch
    2 pizza
    1 sometimes
    1 and
    1 hello
    1 java
    1 like
    1 pizza
    1 programing
    2 and
    1 breakfast
    1 eat
    2 for
    1 lunch
    2 pizza
    1 sometimes
    1 and
    1 hello
    1 java
    1 like
    1 pizza
    1 programing
    2 and
    1 breakfast
    1 eat
    2 for
    1 lunch
    2 pizza
    1 sometimes

    and my code looks like:
    package lab;

    import java.awt.FileDialog;
    import java.awt.Frame;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.*;


    public class NonDuplicates {
    public static void main(String[] args) throws IOException {{

    // Create a TreeMap to hold words as key and count as value

    Frame f = new Frame();
    //decide from where to read the file
    FileDialog foBox = new FileDialog(f,"Reading text file", FileDialog.LOAD);
    foBox.setVisible(true);
    //get the absolute path to the file
    String foName = foBox.getFile();
    String dirPath = foBox.getDirectory();

    // create a file instance for the absolute path
    File inFile = new File(dirPath + foName);
    BufferedReader in=null;

    in= new BufferedReader(new FileReader(inFile));


    String line = in.readLine();


    while(line!=null)
    {

    StringTokenizer t = new StringTokenizer(line,"|");
    {

    String id = t.nextToken().trim();
    // read in the next line
    line=in.readLine();

    TreeMap<String, Integer> map = new TreeMap<String, Integer>();

    String[] words = id.split("[ \n\t\r.,;:!?(){}]");

    for (int i = 0; i < words.length; i++) {
    String key = words[i].toLowerCase();

    if (words[i].length() > 1) {
    if (map.get(key) == null) {
    map.put(key, 1);
    }
    else {
    int value = map.get(key).intValue();
    value++;
    map.put(key, value++);
    }


    }
    }

    // Get all entries into a set
    Set<Map.Entry<String, Integer>> entrySet = map.entrySet();


    // Get key and value from each entry
    for (Map.Entry<String, Integer> entry: entrySet)
    System.out.println(entry.getValue() + "\t" + entry.getKey());

    }
    }
    }
    }
    }

    Where should I start to look??
    Thanks

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I'm trying to write a program that inputs a text file and counts the words.

    Unfortunately, my output isn't counting but displaying only the words with out counting.
    I am not sure I understand this - the output you posted does show word counts. What output were you expecting from that input?

    In fact the input you posted is not the actual intput that producd that output (there should also be lines which set the id). It would be a good idea to use a String to represent the input data rather than reading it from a file: that way we can all see the data that is being used. Once the logic of your program is correct you can add the code that reads the data from a file.

    -----

    Notice that you create a new counting map each time you read a line:

    Java Code:
    line = in.readLine();
    TreeMap<String, Integer> map = new TreeMap<String, Integer>();
    The effect of this is that map will only have word counts for that particular line. If you want to accumulate word counts then map should be initialised outside (before) the while loop and its contents printed after that loop.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    @op: a few suggestions, first, don't use StringTokenizer, it's a legacy class and should not be used in new code. Instead, use split more. Split the line based on | and then loopthrough the produced array and do all the parsing in the loop.

    You are also doing unnecessary setup for in.readLine(), instead of
    Java Code:
    read line
      while
        read line
    Do

    Java Code:
    String s;
    while((s = in.readLine())!= null)
      parse line
    Last edited by sunde887; 07-17-2011 at 03:33 PM.

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by sunde887 View Post
    The op was already using readLine() so that piece of advice is quite useless.
    Wrong end of the stick error in line 1 - pbrockway2 was simply pointing out code containing a mistake the OP was making, not advising him to use the posted extract.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by dlorde View Post
    Wrong end of the stick error in line 1 - pbrockway2 was simply pointing out code containing a mistake the OP was making, not advising him to use the posted extract.
    That actually wasn't directed at pbrock, someone posted telling the op to use readLine after pbrock and deleted it right after my post.

    Edited that bit out since pbrocks advice is not useless and in reality pinpoints the main problem.
    Last edited by sunde887; 07-17-2011 at 03:35 PM.

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by sunde887 View Post
    That actually wasn't directed at pbrock, someone posted telling the op to use readLine after pbrock and deleted it right after my post.
    Oops! I'm sorry, I didn't know... there should be a law against deleting non-duplicate posts
    Last edited by dlorde; 07-17-2011 at 03:46 PM.

Similar Threads

  1. Replies: 3
    Last Post: 11-09-2010, 01:10 AM
  2. Read in words from a text file that contains a story
    By Pleenen in forum Advanced Java
    Replies: 2
    Last Post: 05-27-2010, 05:34 AM
  3. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 05:01 PM
  4. Replies: 3
    Last Post: 03-22-2010, 08:27 PM
  5. Replies: 8
    Last Post: 02-04-2009, 09:55 PM

Posting Permissions

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