I require searching a word in a text file and display the line number using java. If it appears more than once I need to show all the line numbers in the output. Can anyone help me please?
Asked
Active
Viewed 1.6k times
2
-
Search for *grep java* and you'll find lots of examples about how to do this. Here's one [example](http://introcs.cs.princeton.edu/java/72regular/Grep.java.html) (without line numbers, but it should be easy to modify) – Augusto Sep 25 '11 at 20:57
-
No, its small part of a project. – Neel Sep 25 '11 at 21:11
3 Answers
5
Read the text file using Java class LineNumberReader
and call method getLineNumber
to find the current line number.
http://docs.oracle.com/javase/7/docs/api/java/io/LineNumberReader.html

Jonathan W
- 3,759
- 19
- 20

Simon C
- 1,977
- 11
- 14
2
You can store this information manually. Whenever you are invoking readline()
of your BufferedReader
, if you're using such, you can also increment a counter by one. E.g.,
public int grepLineNumber(String file, String word) throws Exception {
BufferedReader buf = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(file))));
String line;
int lineNumber = 0;
while ((line = buf.readLine()) != null) {
lineNumber++;
if (word.equals(line)) {
return lineNumber;
}
}
return -1;
}

laffuste
- 16,287
- 8
- 84
- 91

Johan Sjöberg
- 47,929
- 21
- 130
- 148
-
And to solve the problem at hand, add a snippet in there that if the line contains the word you're looking for, print the line number. – corsiKa Sep 25 '11 at 20:57
-
-
@Ted, good addendum. I'll leave the code for reference as it's already been suggested by Simon. – Johan Sjöberg Sep 25 '11 at 21:01
2
Something like this might work:
public ArrayList<Integer> find(String word, File text) throws IOException {
LineNumberReader rdr = new LineNumberReader(new FileReader(text));
ArrayList<Integer> results = new ArrayList<Integer>();
try {
String line = rdr.readLine();
if (line.indexOf(word) >= 0) {
results.add(rdr.getLineNumber());
}
} finally {
rdr.close();
}
return results;
}

Ted Hopp
- 232,168
- 48
- 399
- 521
-
If I print 'results' will it return all the line numbers if the word exists more than once in the file? I am not getting that with this one. – Neel Sep 25 '11 at 23:30
-
The returned list is supposed to have the line number of every line that contains the word. If you're getting null back from the function (and not an exception), something is very wrong. Can you show your calling code? – Ted Hopp Sep 26 '11 at 00:07
-
I did exactly the same as you specified. The problem is that it is not entering into the 'if' loop, that why it is returning null. But the word is there in the text. – Neel Sep 26 '11 at 00:15
-
Ok, Ted, I solved it. Now 'results' prints the all line numbers and 'results' is an arrayList. Now if I want to print the line numbers of all the words in the text into a single array which will be an array of arrayList, how can I print that array of arrayList? – Neel Sep 26 '11 at 02:29
-
@user350129 - I'm not sure what you are after. Perhaps you have an array of words and you want an array of positions for each word. Is that what you want? – Ted Hopp Sep 26 '11 at 04:57