This repository has been archived by the owner on Jan 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA3.java
64 lines (58 loc) · 2.09 KB
/
PA3.java
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
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
public class PA3 {
public static final int DEBUG = 0;
public static void main(String[] args) {
if (args.length < 1 || args.length >1){
System.out.println("Error: incorrect arguments");
}
else {
//Object to hold word information for given web pages.
WebPages webPage = new WebPages();
//Commands from input file
ArrayList<String> commands = parseInputFile(args[0]);
ArrayList<String> nonEmptyCommands = new ArrayList<String>();
for(String command: commands){
if(command.trim().compareTo("") != 0)
nonEmptyCommands.add(command);
}
commands = nonEmptyCommands;
if (commands.size() == 0) {
System.out.println("Error: empty file");
} else {
//Scan in files until *EOF* command
boolean reachedEOF = false;
for (String command : commands) {
if (command.equals("*EOFs*")) {
webPage.printTerms();
reachedEOF = true;
System.out.println();
continue;
}
if (!reachedEOF) {
webPage.addPage(command);
}
if (reachedEOF) {
webPage.getTermIndex().get(command, true);
}
}
}
}
}
public static ArrayList<String> parseInputFile(String filename) {
ArrayList<String> commands = new ArrayList<String>();
File input = new File(filename);
try {
Scanner scanner = new Scanner(input);
while (scanner.hasNextLine()) {
commands.add(scanner.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return commands;
}
}