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]);
}
}
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]);
}
}
The file name is passed in as the first parameter to the program
ASKER
Do you mean here? public static void main(String args[])
java LineCounter fileName.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Or here?FileReader fr = new FileReader(args[0]);
;-)
ASKER
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
ASKER
I'm using netbeans and not a command line
ASKER
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
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