Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Where do I put the filename to be read?

Posted on 2004-08-16
10
Medium Priority
?
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 86

Expert Comment

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

Author Comment

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

Expert Comment

by:CEHJ
ID: 11810557
java LineCounter fileName.txt
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 11810565
>>Do you mean here?

I mean on the command line as i showed above
0
 

Author Comment

by:lrobinson2001
ID: 11810572
Or here?FileReader fr = new FileReader(args[0]);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11810606
;-)
0
 

Author Comment

by:lrobinson2001
ID: 11810997
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
ID: 11811157
I'm using netbeans and not a command line
0
 

Author Comment

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

Expert Comment

by:CEHJ
ID: 11811445
>>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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

721 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