Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

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]);
   }
}
0
lrobinson2001
Asked:
lrobinson2001
  • 5
  • 5
1 Solution
 
CEHJCommented:
The file name is passed in as the first parameter to the program
0
 
lrobinson2001Author Commented:
Do you mean here? public static void main(String args[])
0
 
CEHJCommented:
java LineCounter fileName.txt
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
>>Do you mean here?

I mean on the command line as i showed above
0
 
lrobinson2001Author Commented:
Or here?FileReader fr = new FileReader(args[0]);
0
 
CEHJCommented:
;-)
0
 
lrobinson2001Author Commented:
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
0
 
lrobinson2001Author Commented:
I'm using netbeans and not a command line
0
 
lrobinson2001Author Commented:
Should I put code to read the file somewhere? Kinda confused. I'm a new at this.
0
 
CEHJCommented:
>>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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now