I am currently stuck with a problem. I am supposed to write a programm that is able to search for a string in a .txt file given as argument. The programm must return the row and the column of the found string. I am struggling to find a way to achieve that and have no idea how to go on. I would be very happy to hear from you.
Here is my attempt in tackling with my task: - I thought about saving the content of a file via a buffered reader in a string array, but that does not seem to work since I cannot define the length of the array from the beginning - I also thought about saving the content of the file via a buffered reader in a string and then split this string in characters. However I am not sure how I will be able to retreieve the rows in the original file then.
This is the non-functional code that I have at the moment:
public class StringSearch{
public static void main(String[] args){
if(args.length > 0){
BufferedReader br = null;
String text = null;
try{
br = new BufferedReader(new FileReader(args[0]));
// attempt of saving the content of the "argument" file in a string array and then in a string
String[] lines = new String[]; // I know this does not work like this
for( int i = 0; i < lines.length; i++){
lines[i] = br.readLine;
text = text + lines[i];
i++;
}
text.split("\r\n");
} catch (IOException ioe){
ioe.printStackTrace();
} finally{
if (br != null) {
try{
br.close();
}catch (IOException ioe){
ioe.printStackTrace();
}
}
}
}
}
}