-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.java
More file actions
53 lines (40 loc) · 1.21 KB
/
Copy pathFileReader.java
File metadata and controls
53 lines (40 loc) · 1.21 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
// --== CS400 File Header Information ==--
// Name: Ian Koh
// Email: iskoh@wisc.edu
// Team: BA
// TA: Brianna Cochran
// Lecturer: Florian Heimerl
// Notes to Grader:
import java.io.File;
import java.util.Scanner;
import java.io.IOException;
import java.util.InputMismatchException;
public class FileReader {
public static void loadFile(CS400Graph<String> network, String filename) throws IOException, InputMismatchException {
File file = new File(filename);
Scanner sc;
try {
sc = new Scanner(file);
} catch (IOException e) {
throw new IOException("Unable to access file");
}
String line = "";
String[] elems = null;
while(sc.hasNextLine()) {
line = sc.nextLine().trim();
if (line.contains(",")) {
elems = line.split(",");
for (int i = 0; i < elems.length; i++)
elems[i] = elems[i].trim();
if (elems.length < 4) throw new InputMismatchException("Invalid data format");
network.insertVertex(elems[0]);
network.insertVertex(elems[1]);
for (int i = 3; i < elems.length; i++) {
CustomEdge.insertCustomEdge(network, elems[0], elems[1], Integer.parseInt(elems[2]), Integer.parseInt(elems[i]));
}
} else {
network.insertVertex(line);
}
}
}
}