Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with LineNumber Reader class

 
Elvis Ve
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Elvis Ve
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic