aspose file tools*
The moose likes Java in General and the fly likes Help with LineNumber Reader class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help with LineNumber Reader class" Watch "Help with LineNumber Reader class" New topic
Author

Help with LineNumber Reader class

Elvis Ve
Greenhorn

Joined: May 17, 2004
Posts: 19
hey guys how are u all doing? Im having problems using the LineNumberReader class in this follwing method that you will see im trying to print out the line number in front of the actual line of text and all i get is this:
99nulli beg for this thing to work
work is good


this is the method:

void grep(String t)
{
String s = "";
try
{
b = new BufferedReader(new FileReader(f));
c = new LineNumberReader(new FileReader(f));

while((s=b.readLine())!=null)
{
search(t,s);
if (search(t,s)==true)
line = c.getLineNumber()+ '\t' + line + s + '\n';
}




}
catch(FileNotFoundException e )
{
e.printStackTrace();
}
catch(IOException d)
{
d.printStackTrace();
}
finally
{
try
{
b.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}


}

please help, and thanks in advance
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Umm...I'm sure that u got some misunderstanding in these following snippets...


Why do u need both BufferedReader and LineNumberReader for a File? Since LineNumberReader is a subclass of BufferedReader, it can use all methods from its superclass...

If you can provide the whole program and the requirement specification of your program, it would be more likely that we can help you very effectively... Garbage in, Garbage out! If you just provide a small code snippet, it's pretty difficult to know the requirement of your program... So I do hope I can help, if you provide more information...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Elvis Ve
Greenhorn

Joined: May 17, 2004
Posts: 19
sorry, let me try to be more specific In the program the method grep accepts a String, as soon as you call grep it creates a new BufferedReader of the file, then in the while loop it searches line by line with the search method to find the String in a line, now when it s over it returns the lines that it found the String in but my question is how do also return the line number, here is my search method along with the grep method , hope you can help, thanks in advance:

boolean search(String key, String argument)
{
String s = "";
boolean found = false;
StringTokenizer tok = new StringTokenizer(argument);
while(tok.hasMoreTokens())
{
s = tok.nextToken();
if(key.equalsIgnoreCase(s))
{
found = true;
}
}


return found;


}

void grep(String t)
{
String s = "";
int lineNum = 0;
try
{
b = new BufferedReader(new FileReader(f));
c = new LineNumberReader(new FileReader(f));

while((s=b.readLine())!=null)
{
search(t,s);
if (search(t,s)==true)
line = c.getLineNumber()+ '\t' + line + s + '\n';
}




}
catch(FileNotFoundException e )
{
e.printStackTrace();
}
catch(IOException d)
{
d.printStackTrace();
}
finally
{
try
{
b.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}


}
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Try to change your code from


to


where fileLocation is the string of the path to your file...

Hope it helps...
[ June 28, 2004: Message edited by: Ko Ko Naing ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with LineNumber Reader class
 
Similar Threads
how to test functions who use java io classes like LineNumberReader and FileReader
declaring array of vectors
FileIO
Use of File Class...
search files in directory for string and return count