Question

File Processing and Arrays Create a class named FilesAndArrays. Download the sidewalk.txt file so it is...

File Processing and Arrays

Create a class named FilesAndArrays.

Download the sidewalk.txt file so it is in your project’s root folder.

Write a static method named findFirstMatch that accepts as its parameters a Scanner for an input file and an array of Strings keywords representing a list of keywords in a search. Your method will read lines from its input Scanner and should return the line number of the first line in the file that contains one or more words from keywords. If none of the keywords are found in the file, your method should return a -1. The search should be case-insensitive, so if a keyword was "banana", the line "yummy baNAna split" would be considered a line that contains the keyword. Your method should also match whole words only, so if the only keyword in the array was "ball", the line "football game" would not be considered a match.

For example, consider the following input file saved in sidewalk.txt, consisting of 6 lines:

Let us leave this place where the smoke blows black

And the dark street winds and bends.

Past the pits where the asphalt flowers grow

We shall walk with a walk that is measured and slow,

And watch where the chalk-white arrows go

To the place where the sidewalk ends.

The following table shows some calls to your method and their expected results with the following Scanner:

Scanner input = new Scanner(new File("sidewalk.txt"));

Array

Separate Calls / Returned Value

String[] k1 = {"place", "winds"};

findFirstMatch(input, k1) returns 1

String[] k2 = {"dinosaur", "PITS", "pots"};

findFirstMatch(input, k2) returns 3

String[] k3 = {"chalk", "row", "g", "ends"};

findFirstMatch(input, k3) returns -1

String[] k4 = {"to"};

findFirstMatch(input, k4) returns 6

You may assume that none of the words in the keywords array contain spaces, i.e. all keywords are single whole words, and the array contains at least one element. Do not modify the elements of the keywords array.

Once you have verified the individual calls work, in your main method make all the calls. Did your results change? If so – fix your code so the returns are the correct values.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

import java.io.*;

import java.util.*;

public class FilesAndArrays{

public static void main(String[] args) {

try{

Scanner input = new Scanner(new File("sidewalk.txt"));

String[] k1 = {"place", "winds"};

System.out.println(findFirstMatch(input, k1));

input = new Scanner(new File("sidewalk.txt"));

String[] k2 = {"dinosaur", "PITS", "pots"};

System.out.println(findFirstMatch(input, k2));

input = new Scanner(new File("sidewalk.txt"));

String[] k3 = {"chalk", "row", "g", "ends"};

System.out.println(findFirstMatch(input, k3));

input = new Scanner(new File("sidewalk.txt"));

String[] k4 = {"to"};

System.out.println(findFirstMatch(input, k4));

}catch(FileNotFoundException e){

System.out.println("Unable to open file\n");

}

}

public static int findFirstMatch(Scanner fin, String[] keywords){

int line_no = 1;

while(fin.hasNextLine()){

String str[] = fin.nextLine().split(" ");

for(String st: str){

for(String s: keywords){

if(st.equalsIgnoreCase(s))

return line_no;

}

}

line_no++;

}

return -1;

}

}

Add a comment
Know the answer?
Add Answer to:
File Processing and Arrays Create a class named FilesAndArrays. Download the sidewalk.txt file so it is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Tokenizer.java file should contain: A class called: Tokenizer Tokenizer should have a private variable that...

    The Tokenizer.java file should contain: A class called: Tokenizer Tokenizer should have a private variable that is an ArrayList of Token objects. Tokenizer should have a private variable that is an int, and keeps track of the number of keywords encountered when parsing through a files content. In this case there will only be one keyword: public. Tokenizer should have a default constructor that initializes the ArrayList of Token objects Tokenizer should have a public method called: tokenizeFile tokenizeFile should...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • Description: This Java program will read in data from a file students.txt that keeps records of...

    Description: This Java program will read in data from a file students.txt that keeps records of some students. Each line in students.txt contains information about one student, including his/her firstname, lastname, gender, major, enrollmentyear and whether he/she is a full-time or part-time student in the following format. FIRSTNAME      LASTNAME        GENDER              MAJORENROLLMENTYEAR FULL/PART The above six fields are separated by a tab key. A user can input a keyword indicating what group of students he is searching for. For example, if...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

  • Lab 19 - History of Computer Science, A File IO Lab FIRST PART OF CODING LAB:...

    Lab 19 - History of Computer Science, A File IO Lab FIRST PART OF CODING LAB: Step 0 - Getting Starting In this program, we have two classes, FileIO.java and FileReader.java. FileIO.java will be our “driver” program or the main program that will bring the file reading and analysis together. FileReader.java will create the methods we use in FileIO.java to simply read in our file. Main in FileIO For this lab, main() is provided for you in FileIO.java and contains...

  • Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this...

    Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this file, include your assignment documentation code block. After the documentation block, you will need several import statements. import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; Next, declare the Lab12 class and define the main function. public class Lab12 { public static void main (String [] args) { Step 2: Declaring Variables For this section of the lab, you will need to declare...

  • The outputs are very close to the correct values so I assume I just typed an...

    The outputs are very close to the correct values so I assume I just typed an equation incorrectly, but I have not been able to find the error. Courses LMS integration Documentation Write a function to implement the 4th order Runge Kutta method function [ty] - r4(f, range, ich) where is an anonymous function that defines the differential equation range is a vector of length 2 that sets a time range for a range for the independent art the initial...

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Finds the specified set of words in the specified file and returns them * as an ArrayList. This finds the specified set in the file which is on the * line number of the set. The first line and set in the file is 1. * * This returns an ArrayList with the keyword first, then : and then followed...

  • Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains...

    Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains a Java program. Your program should read the file (e.g., InputFile.java) and output its contents properly indented to ProgramName_Formatted.java (e.g., InputFile_Formatted.java). (Note: It will be up to the user to change the name appropriately for compilation later.) When you see a left-brace character ({) in the file, increase your indentation level by NUM_SPACES spaces. When you see a right-brace character (}), decrease your indentation...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT