Reading contents for a text file and couting words

Discussion in 'Java' started by jazzy, May 25, 2004.

  1. jazzy

    jazzy Guest

    Hi,
    I am trying to write a Java program to read a text file and then count the
    number of words in that file. I keep on getting error.
    Thanks

    -JM
    Here is the sample code.


    import java.util.*;
    import java.lang.*;
    import java.io.*;

    public class ReadFile {
    public static void main(String[] args)

    {
    //ReadFile Myfile.txt
    int totalWords = 0;
    String line;
    String word;
    // Open and Read the Myfile.txt

    try {
    BufferedReader in = new BufferedReader(new
    FileReader("Myfile.txt"));
    String str;
    while ((str = in.readLine()) != null) {
    //Process string
    }
    in.close();
    } catch (IOException e) {

    }
    StringTokenizer str = new StringTokenizer(line, " \t\n\r");
    while(str.hasMoreTokens())
    {
    totalWords++;
    word = str.nextToken();
    }
    }
    }
     
    jazzy, May 25, 2004
    #1
    1. Advertising

  2. jazzy

    Kevin C. Guest

    "jazzy" <> wrote in message
    news:l6Bsc.3741$...
    > Hi,
    > I am trying to write a Java program to read a text file and then count the
    > number of words in that file. I keep on getting error.
    > Thanks


    _What_ error?
     
    Kevin C., May 25, 2004
    #2
    1. Advertising

  3. On Tue, 25 May 2004 06:23:11 GMT, Kevin C. wrote:

    > "jazzy" <> wrote in message
    > news:l6Bsc.3741$...
    >> Hi,
    >> I am trying to write a Java program to read a text file and then count the
    >> number of words in that file. I keep on getting error.
    >> Thanks

    >
    > _What_ error?


    That is a very good question Kevin.
    <http://www.physci.org/codes/javafaq.jsp#exact>

    To the OP, try this compileable variant of
    your code and you may begin to understand..

    <sscce>
    import java.util.*;
    import java.lang.*;
    import java.io.*;

    public class ReadFile {

    public static void main(String[] args) {
    //ReadFile Myfile.txt
    int totalWords = 0;
    String line = "";
    String word;
    // Open and Read the Myfile.txt

    try {
    BufferedReader in = new BufferedReader(new
    FileReader("Myfile.txt"));
    String str;
    while ((str = in.readLine()) != null) {
    //Process string
    }
    in.close();

    StringTokenizer st = new StringTokenizer(line, " \t\n\r");
    while(st.hasMoreTokens()) {
    totalWords++;
    word = st.nextToken();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    </sscce>

    Also have a look over this..
    <http://www.physci.org/codes/sscce.jsp>
    (your code did not compile)

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, May 25, 2004
    #3
  4. On Tue, 25 May 2004 05:48:01 GMT, jazzy wrote:
    > I am trying to write a Java program to read a text file and then
    > count the number of words in that file. I keep on getting error.


    Exactly *what* error do you get?

    > import java.util.*;
    > import java.lang.*;


    It is never necessary to import classes from java.lang.

    > import java.io.*;
    >
    > public class ReadFile {
    > public static void main(String[] args)
    > {
    > //ReadFile Myfile.txt
    > int totalWords = 0;
    > String line;
    > String word;
    > // Open and Read the Myfile.txt
    >
    > try {
    > BufferedReader in = new BufferedReader(new
    > FileReader("Myfile.txt"));


    Suggestion: get the filename from the command line. Do you intend to
    edit and recompile the program each time you want to count the words
    in a different file?

    > String str;
    > while ((str = in.readLine()) != null) {
    > //Process string


    You forgot to actually process the String here. Perhaps you need to
    count the words on this line, before continuing with the next one.

    > }
    > in.close();
    > } catch (IOException e) {


    It's a good idea to either handle or report this exception if it
    occurs, not just hide it (which the empty catch block does).

    > }


    I suspect this is the String processing code you had intended to
    invoke in the above loop. You can either move it there, or make it
    into a separate method, which you call from there:

    > StringTokenizer str = new StringTokenizer(line, " \t\n\r");
    > while(str.hasMoreTokens())
    > {
    > totalWords++;
    > word = str.nextToken();
    > }
    > }
    > }


    You might also be interested to know that StringTokenizer can count
    the tokens for you, you don't need to iterate over them yourself.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, May 25, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Schumacher

    couting cout.

    Chris Schumacher, Apr 13, 2004, in forum: C++
    Replies:
    1
    Views:
    376
    Buster
    Apr 13, 2004
  2. sahm
    Replies:
    4
    Views:
    42,689
    rel0aded911
    Nov 23, 2009
  3. Jochus
    Replies:
    1
    Views:
    786
    John Ratliff
    Oct 30, 2005
  4. Pali
    Replies:
    10
    Views:
    1,348
    Default User
    Nov 8, 2010
  5. Mangler

    Couting Days

    Mangler, Mar 7, 2007, in forum: ASP General
    Replies:
    2
    Views:
    183
    Mangler
    Mar 7, 2007
Loading...

Share This Page