-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
67 lines (64 loc) · 2.69 KB
/
Driver.java
File metadata and controls
67 lines (64 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* This Driver class test the HorspoolStringSearch class.
* It will ask user to enter a file name to open a text file
* and ask for the word the user want to search for.
* It will also give user the option to ignore the case.
* At the end it outputs the number of the occurrences of the searchString,
* and which lines the subString was found on.
*
*/
import java.io.*;
import java.util.*;
import java.lang.String;
public class Driver
{
public static void main(String[] args) throws IOException
{
//create an object for Scanner class
Scanner keyboard = new Scanner (System.in);
//these are the fields
String fileName;
String searchString;
String sentence;
int lineNum=0;
int result=0;
int totalResult = 0;
// create an object of the HorspoolStringSearch class
HorspoolStringSearch stm = new HorspoolStringSearch();
//ask user to input the name and location of the text file
System.out.println("Enter the input file name and location: ");
fileName = keyboard.nextLine();
// creates an Scanner object to read user's input
Scanner inputFile = new Scanner (new File(fileName));
//call the askCase method to get their choice of ignore case or not
char ignoreCase = stm.askCase();
//ask user for the word they want to search for
System.out.println("What word would you like to search for?");
searchString = keyboard.nextLine();
//This loop will step throught every line of the text until it reaches the end
while (inputFile.hasNext())
{
//assigns each line to variable sentence
sentence = inputFile.nextLine();
//if user decides to ignored case, convert both sentence and searchString to LowerCase
if(ignoreCase == 'y'){
sentence = sentence.toLowerCase();
searchString = searchString.toLowerCase();
}
// calls the search method of the HorspoolStringMatcher class
stm.search(sentence, searchString);
//System.out.println(stm.getMatches());
result = stm.horspoolSearch();
if (result > 0)
{
System.out.println("On line " + (lineNum + 1) + " it was found: " + result + " times");
totalResult += result;
result = 0;
}
//increase the line number
lineNum++;
}
//Displays the output
System.out.println("Total found: " + totalResult);
}
}