In: Computer Science
search the file "myfile.txt" for any occurrences of "Some target string" and print the lines where the string is found.
In this assignment, write your own java version of this program:
To complete this assignment you will need to use Scanner, File, and String. Consider reviewing the documentation for these classes. The attached file is provided as an example text file for developing and testing your program.
If you have any doubts, please give me comment...
import java.io.*;
import java.util.Scanner;
public class WhiteMax1{
public static void main(String[] args) throws FileNotFoundException{
if(args.length!=2){
System.out.println("Usage: java WhiteMax1.java <Some target String> filename");
System.exit(0);
}
Scanner fileScnr = new Scanner(new File(args[1]));
String line;
String searchWord = args[0];
int line_no = 1, pos;
while(fileScnr.hasNextLine()){
line = fileScnr.nextLine();
pos = line.indexOf(searchWord);
while(pos!=-1){
System.out.println(line_no+":"+pos+" "+line);
pos = line.indexOf(searchWord, pos+1);
}
line_no++;
}
fileScnr.close();
}
}