Link to home
Start Free TrialLog in
Avatar of lrobinson2001
lrobinson2001

asked on

Where do I put the filename to be read?

I trying to use a stream tokenizer to count the number of lines in a text file using the eolIsSignificant method(). Where do I put the filename to be read? This compiles OK but of course I'm not sure while to put the file name so I it print no lines.


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

public class LineCounter {
  public static void main(String args[])
  {
    if (args.length == 0) {
      System.err.println("no lines");
      System.exit(1);
    }
    int lineCount = 0;
    try {
      FileReader fr = new FileReader(args[0]);
      BufferedReader br = new BufferedReader(fr);
      StreamTokenizer st = new StreamTokenizer(br);
      st.eolIsSignificant(true);
      int type;
      while ((type = st.nextToken()) != StreamTokenizer.TT_EOF) {
        if (type == StreamTokenizer.TT_EOL){
          ++lineCount;
        }
      }
      br.close();
      // Always one more line than EOLs
        if (lineCount > 0){
          ++lineCount;
        }
      }
      catch (IOException e) {
        System.err.println(e);
      }
      System.out.println("There are " + lineCount + " lines in file " + args[0]);
   }
}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

The file name is passed in as the first parameter to the program
Avatar of lrobinson2001
lrobinson2001

ASKER

Do you mean here? public static void main(String args[])
java LineCounter fileName.txt
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or here?FileReader fr = new FileReader(args[0]);
;-)
I still must be putting in the wrong place. Can you show where you mean. I am trying to read a file I put in my c:\Temp\readfile.text
I'm using netbeans and not a command line
Should I put code to read the file somewhere? Kinda confused. I'm a new at this.
>>I'm using netbeans and not a command line

Then you must find out, if you're using an IDE, what command line the IDE is using to run the class and how to change it