-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocument.java
More file actions
118 lines (93 loc) · 3.54 KB
/
Document.java
File metadata and controls
118 lines (93 loc) · 3.54 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* CSC-239 Project 4: Text Document Index Generator
* Student: Lukas Correia
* Date: 12/1/2021
* Description: This program reads a text file and produces a display
* for all distinct words in file, along with the line number,
* and word number values for each location where the word
* appears.
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Document {
// arrayList of Word object.
private ArrayList<Word> wordArrayList;
// arrayList of Strings (i.e. the actual lowercase word of the word object in
// the arrayList).
private ArrayList<String> wordList;
// File name variable.
private File fileName;
// No args constructor.
public Document() {
this.wordArrayList = null;
this.fileName = null;
this.wordList = null;
}
// Overloaded constructor.
public Document(File fileName) {
this.wordArrayList = new ArrayList<>();
this.fileName = fileName;
this.wordList = new ArrayList<>();
}
public static void main(String[] args) throws FileNotFoundException {
// Creates new Scanner object.
Scanner sc = new Scanner(System.in);
// Prompts user for filename.
System.out.print("Enter fileName: ");
// Takes the next user input on the command line.
String input = sc.nextLine();
// Creates a new file object with the user input from command line.
File fileName = new File(input);
// Create new Scanner object reading in the File fileName as argument.
Scanner scf = new Scanner(fileName);
// Creates a new Document object taking the File fileName as argument.
Document doc = new Document(fileName);
// Current line number of the file (set at 1).
int lineNumber = 1;
// While there's still a line in the file to be processed
while (scf.hasNextLine()) {
// Load the line in.
String line = scf.nextLine();
// Split up with the given regex into a String array called words.
String[] words = line.toLowerCase().replaceAll("[^a-zA-Z ]", "").split(" ");
// For loop to go through the word array.
for (int i = 0; i < words.length; i++) {
// Create a new Word object (temporary).
Word w = new Word(words[i], lineNumber, i + 1);
// Check the new Word object's word variable,
// and if it exists in the arrayList wordList,
// save the index of the existing word within wordList.
if (doc.wordList.contains(w.getWord())) {
int index = doc.wordList.indexOf(w.getWord());
// Go to the Word object of the saved index within the wordArrayList
// and add to its Occurrence arrayList
doc.wordArrayList.get(index).addOccurrence(lineNumber, i + 1);
}
// If the new Word object's word variable does not exist
// in the arrayList wordList,
// add the new Word object's word variable to wordList,
// and add the new Word object itself to wordArrayList.
else {
doc.wordList.add(w.getWord());
doc.wordArrayList.add(w);
}
}
// Increase lineNumber to track what line we are currently on.
lineNumber++;
}
// Sorts the wordArrayList alphabetically
doc.wordArrayList.sort(new Comparator<Word>() {
@Override
public int compare(Word o1, Word o2) {
return o1.getWord().compareTo(o2.getWord());
}
});
// Prints out output
for (int i = 0; i < doc.wordArrayList.size(); i++) {
System.out.printf("%s(%d): " + doc.wordArrayList.get(i).getOccList(), doc.wordArrayList.get(i).getWord(),
doc.wordArrayList.get(i).getOccurrence());
System.out.println();
}
}
}