Solved

Where do I put the filename to be read?

Posted on 2004-08-16
10
202 Views
Last Modified: 2010-03-31
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
Comment
Question by:lrobinson2001
  • 5
  • 5
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
The file name is passed in as the first parameter to the program
0
 

Author Comment

by:lrobinson2001
Comment Utility
Do you mean here? public static void main(String args[])
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
java LineCounter fileName.txt
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
Comment Utility
>>Do you mean here?

I mean on the command line as i showed above
0
 

Author Comment

by:lrobinson2001
Comment Utility
Or here?FileReader fr = new FileReader(args[0]);
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
;-)
0
 

Author Comment

by:lrobinson2001
Comment Utility
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
 

Author Comment

by:lrobinson2001
Comment Utility
I'm using netbeans and not a command line
0
 

Author Comment

by:lrobinson2001
Comment Utility
Should I put code to read the file somewhere? Kinda confused. I'm a new at this.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now