Re: hi
Lab.Bhattacharjee@gmail.com wrote:
Hi kavitha,
u can try out the following code , it allows wild cards ,but is case
sensitive.Please tell me if u need that too??
The word is "you", not "u".
The trouble with doing someone's homework for them is that you deprive them of
an education. That's a big waste of their tuition.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RnD {
public static void main(String[] args) {
Please do not embed TABs in Usenet posts.
Pattern pattern = Pattern.compile("SAXELBY");
What is this regex? How did you come up with it? There was no indication in
the OP for this.
BufferedReader reader = null;
try {
int lineNumber=0;
reader =
new BufferedReader(
new FileReader("C:\\00000001.txt"));
Hard-coded strings. Tsk, tsk. Where did you come up with it? There was no
indication in the OP for this.
while (true) {
To depend on exceptions as flow control is a bad practice.
lineNumber++;
String curline = reader.readLine();
if (curline == null) {
break;
}
Matcher matcher = pattern.matcher(curline);
while (matcher.find()) {
System.out.println("at "+lineNumber+" "+
matcher.group()
+ " start="
+ matcher.start()
+ " end= "
+ matcher.end());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
Ooh, you ignore the exception and move on. This is good for examples, bad in
practice.
This is also a good place to introduce logging statements.
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
Those darn TABs.
}
}
}
}
}
- Lew