-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
134 lines (118 loc) · 4.73 KB
/
Main.java
File metadata and controls
134 lines (118 loc) · 4.73 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Angelo Espiritu
3/5/2026
Purpose: To create a program to sort through a CSV find out which ACE value was maximal and output to file, this time using a linked list and sorting by CSV
Sources:
https://www.wscubetech.com/resources/dsa/doubly-linked-list-data-structure
https://www.tutorialspoint.com/data_structures_algorithms/doubly_linked_list_algorithm.htm
https://www.geeksforgeeks.org/dsa/introduction-to-doubly-linked-lists-in-java/
https://www.tutorialspoint.com/data_structures_algorithms/doubly_linked_list_algorithm.htm
https://stackoverflow.com/questions/55228367/how-to-write-a-tostring-method-using-nodes-in-java
https://www.javaspring.net/blog/java-doubly-linked-list/
https://www.youtube.com/watch?v=bQlw5tJM6_0&list=PLt4nG7RVVk1gIcVQAo8laecQWkzOdYe6i
https://www.youtube.com/watch?v=N6dOwBde7-M
https://www.geeksforgeeks.org/dsa/insertion-in-linked-list/
https://www.geeksforgeeks.org/dsa/deletion-in-linked-list/
This one was hard for me so I got help from a friend at my university (Ethan Lin)
*/
import java.util.*; //imports all package in util
import java.io.*; //imports all methods in IO
//Game plan for main:
//1.) read ace.csv and put into an array list
//2.) parse through ace.csv and store that into arraylist
//3.) write max method
//4.) write text and print statements
public class Main
{
public static void main(String[] args)
{
String csvFile = "ace.csv";
//ArrayList to store HurricaneRowData
//ArrayList<HurricaneRowData> dataList = new ArrayList<>();
DoublyLinkedSortedList dataList = new DoublyLinkedSortedList();
//linkedlist tests
String test1 = dataList.testFirst();
String test2 = dataList.testRemove();
System.err.println(test1);
System.out.println(test2);
//reads in ace.csv
try
{
//puts variable csvFile in a new variable f in object File
File f = new File(csvFile);
//scanner reads in file
Scanner s = new Scanner(f);
s.nextLine(); //skips header
//stores each line of ace
ArrayList<String> lines = new ArrayList<>();
//while the scanner has a next line, read nextline
while (s.hasNextLine())
{
lines.add(s.nextLine());
}
s.close();
//for loop to go through each line of ace
for (int i = 0; i < lines.size(); i++)
{
//splits the csv by comma
String[] parts = lines.get(i).split(",");
//parses by line
int year = Integer.parseInt(parts[0]);
int ace = Integer.parseInt(parts[1]);
int namedStorms = Integer.parseInt(parts[2]);
int hurricanes = Integer.parseInt(parts[3]);
int majorHurricanes = Integer.parseInt(parts[4]);
//instantiate HurricaneRowData variable named row
HurricaneRowData row = new HurricaneRowData(year, ace, namedStorms, hurricanes, majorHurricanes);
//store to row (changed from add to insert)
dataList.insert(row);
}
} //if file not found
catch(Exception e)
{
System.out.println("File not found");
System.out.println(e);
e.printStackTrace();
System.exit(1);
}
//create a variable to store where maxRow is (don't need since i can get first node)
//HurricaneRowData maxRow = getFirst(dataList);
//copy and pasted from instructions with my tweaks in the output screen
Node link = dataList.getFirst();
HurricaneRowData dat = link.getValue();
int max_year = dat.getYear();
System.out.println("Year of max ace: "+max_year);
System.out.println("All data in order of Ace:");
System.out.println(dataList);
//outputs hurricane.txt
try
{
FileWriter fw = new FileWriter("hurricane.txt");
fw.write("Year with maximum ACE: " + max_year + "\n");
fw.write("All data in order of Ace: \n");
fw.write(dataList.toString());
fw.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
//Game plan for findMaxAce
//Use for loop where i = 1 (assumes that index 0 is largest in max variable) to go through entire CSV
//if the ace index is larger, replace it with index
//return the max
public static HurricaneRowData findMaxAce(ArrayList<HurricaneRowData> list)
{
//variable to store max
HurricaneRowData max = list.get(0);
for(int i = 1; i<list.size(); i++)
{
if (list.get(i).getAceIndex() > max.getAceIndex())
{
max = list.get(i);
}
}
return max;
}
}