-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMidterm.java
111 lines (90 loc) · 3.53 KB
/
Midterm.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
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
import java.awt.geom.Area;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Midterm {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Please enter the file name or type QUIT to exit:");
Scanner keyboard = new Scanner(System.in);
String fileInput = "input1.txt"; //keyboard.nextLine();
File file = new File(fileInput);
Scanner inputFile = new Scanner(fileInput);
fileInput.toLowerCase();
String flagQuit = "quit";
if (!inputFile.hasNext()){
System.out.printf("File '%s' is empty.\n", fileInput);
System.exit(0);
inputFile.close();
}
inputFile.close();
if (fileInput.equals(flagQuit)) {
inputFile.close();
System.exit(0);
}
while (!file.exists()) {
System.out.printf("File %s is not found.\n", fileInput);
System.out.println("Please re-enter the file name or type QUIT to exit:");
fileInput = keyboard.nextLine();
file = new File(fileInput);
if (fileInput.equals(flagQuit)) {
inputFile.close();
System.exit(0);
}
}
ArrayList<String> arrayDatafile = load(file);
//System.out.println(arrayDatafile);
//System.out.println(countTheOccurrences(arrayDatafile, "Michael"));
for (int i = 0; i < countElements(file); i++) {
String element= (String) arrayDatafile.get(i);
int occurrence = countTheOccurrences(arrayDatafile, element);
if (occurrence >=2) {
indexOf(arrayDatafile, element);
}
}
System.out.printf("Total of %d matches found.\n", totalMatches(arrayDatafile));
}
private static int totalMatches(ArrayList<String> list ){
int count = 0;
for (int i = 0; i < list.size(); i++) {
String element = (String) list.get(i);
int occurrence = countTheOccurrences(list, element);
if (occurrence >=2) {
count++;
}
}
return count;
}
private static void indexOf(ArrayList<String> list, String element){
System.out.printf("Match found: '%s' on lines %d and %d.\n", element, list.indexOf(element) +1, list.lastIndexOf(element) +1);
}
private static int countTheOccurrences(ArrayList<String> list, String element ){
int theCount = 0;
for (int i =0; i < list.size(); i++){
if (element.equals(list.get(i))){
theCount++;
}
}
return theCount;
}
private static int countElements(File file) throws FileNotFoundException {
int acumullator = 0;
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
String fileData = inputFile.nextLine();
acumullator++;
}
inputFile.close();
return acumullator;
}
private static ArrayList<String> load(File file) throws FileNotFoundException {
Scanner inputFile = new Scanner(file);
ArrayList<String> result = new ArrayList<String>(countElements(file));
for (int index = 0; index < countElements(file); index++){
result.add(inputFile.nextLine());
}
inputFile.close();
return result;
}
}