Question

In: Computer Science

search the file "myfile.txt" for any occurrences of "Some target string" and print the lines where...

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:

  1. Name the program LastnameFirstname1 (this is not really an appropriate name for such a program, but following this convention greatly eases the grading process). Example: WhiteMax1
  2. Allow the user to specify the string they are searching for and the file they wish to search with command line arguments. To replicate the above findstr and grep examples, the program would be executed with the following command: java WhiteMax1 "Some target string" myfile.txt  (for help using command line arguments, see this article).
  3. Search each line of the file for the target string. For every occurrence found, first print out the line number where it has been found, the index of the target string's start position in that line, and the entire line itself. For example, searching line 5, "My dog is a good boy" for the target string "dog" would print: 5:3 My dog is a good boy   (If no occurrences are found, nothing needs to be printed. If multiple occurrences are found in the same line, print the line multiple times with each index position. File line numbers start at 1, not 0)

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.

Solutions

Expert Solution

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();

    }

}


Related Solutions

Problem: Take 3 inputs string, integer, string. If incorrect number of lines are given then print...
Problem: Take 3 inputs string, integer, string. If incorrect number of lines are given then print an error message. Also have to make sure 2nd input is an integer and not a string by default. The function in the program should take the second string and insert it into the first string in the position indicated by the integer input given. The program cannot use strlen() or strncpy() functions and it should check the integer input to make sure it...
Use your favorite text editor or IDE to search for occurrences of setState. Where you found...
Use your favorite text editor or IDE to search for occurrences of setState. Where you found uses of setState, refactor the code to use JavaScript functions instead of classes and the useState hook instead of setState. import React from 'react' import ColorSlider from './ColorSlider' import ColorOutput from './ColorOutput' import styles from './ColorBrowser.module.css' class ColorBrowser extends React.Component { constructor(props) { super(props) this.state = { red: Math.floor(Math.random() * 256), green: Math.floor(Math.random() * 256), blue: Math.floor(Math.random() * 256) } } updateColor(e) { this.setState({...
Use the Main.java file below to read in phrases (entire lines!) and print underscores "_" for...
Use the Main.java file below to read in phrases (entire lines!) and print underscores "_" for each character. Main.java already imports the Scanner class, creates your Scanner object, and reads in the first phrase from the keyboard. Your task is as followed: import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner keyboard = new Scanner(System.in);        String phrase = keyboard.nextLine();           //Your code here!    } } Write a nested...
How to read and print all contents in a binary file using a Binary Search Tree...
How to read and print all contents in a binary file using a Binary Search Tree inorder traversal in C. Additionally, how to search using a Binary Search Tree to display the specific Athlete and his/her information. An example would be a sports record binary file that consist of name of athlete, height , weight, championships won. Athlete: Michael Jordan Height: 6’6” Weight : 205 lbs Championships won: 6 =================== Athlete: LeBron James Height: 6’8” Weight: 250 lbs Championships won:...
Write a C program that can search for a string within a text file, replace it...
Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.
Using Python #Write a function called after_second that accepts two #arguments: a target string to search,...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search, and string to search #for. The function should return everything in the first #string *after* the *second* occurrence of the search term. #You can assume there will always be at least two #occurrences of the search term in the first string. # #For example: # after_second("11223344554321", "3") -> 44554321 # #The search term "3" appears at indices 4 and 5. So, this #returns everything...
(In JAVA)Write code to print the location of any alphabetic character in the 2-character string passCode.
Java Language Write code to print the location of any alphabetic character in the 2-character string passCode. Each alphabetic character detected should print a separate statement followed by a newline. Ex: If passCode is "9a", output is: Alphabetic at 1 import java.util.Scanner;   public class FindAlpha {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       String passCode;              passCode = scnr.next();     ...
Program Language C++ How do I take lines of string from an input file, and implement...
Program Language C++ How do I take lines of string from an input file, and implement them into a stack using a double linked list? Example input, command.txt, and output file. Ex: Input.txt postfix: BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D / E+ Command.txt printList printListBackwards Output.txt List: postfix:BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D/E+ Reversed List: postfix:AB-CF*-D/E+ postfix:AB-C-D/ postfix:FE-C/B*A+ prefix:+A*B/C-EF postfix:BAC-*
1)     The provided sched.cpp file is missing a few lines to read in some of the data....
1)     The provided sched.cpp file is missing a few lines to read in some of the data. The comments in the code describe how there is yet another stream library that works like iostream and fstream, but for processing string’s. Fill in the necessary statements and uncomment the indicated cout statements to get the output below for the attached “sched.txt” file: CS100 Section 1 has 17 open seats It is held on MW from 8:00A to 9:15A in SC-S146 It is...
Conduct a web search for some examples of companies/organisations where internal controls have Failed to operate...
Conduct a web search for some examples of companies/organisations where internal controls have Failed to operate effectively. You can also look at some of the corporate collapses including failures of Any banking institutions. Examples can also be a non-profit organisation such as football club or Hospital etc. Your selected case would have received media publicity or there would have been some Government investigation. For the cases you have identified, answer the following questions: (a) What factors led to the failure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT