-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataWrangler.java
More file actions
80 lines (73 loc) · 2.07 KB
/
Copy pathDataWrangler.java
File metadata and controls
80 lines (73 loc) · 2.07 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
// --== CS400 File Header Information ==--
// Name: Brian Wang
// Email: bwang338@wisc.edu
// Team: BA
// TA: Brianna Cochran
// Lecturer: Gary Dahl
// Notes to Grader: None
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class DataWrangler {
protected static RedBlackTree<Student> importData(String filename){
RedBlackTree<Student> roster = new RedBlackTree<Student>();
File file = new File(filename);
Scanner scan;
try {
scan = new Scanner(file);
} catch (Exception e) {
System.out.println("We cannot find the file you inputted.");
return null;
}
String data;
String[] studentInfo;
String name;
int id;
double gradeInClass;
while (scan.hasNextLine()) {
data = scan.nextLine();
studentInfo = data.split(", ");
if (checkFile(studentInfo)) {
name = studentInfo[0];
id = Integer.parseInt(studentInfo[1]);
gradeInClass = Double.parseDouble(studentInfo[2]);
roster.insert(new Student(name, id, gradeInClass));
}
}
scan.close();
return roster;
}
private static boolean checkFile(String[] data) {
if (data.length != 3) {
return false;
}
return true;
}
protected static boolean exportData(RedBlackTree<Student> roster, String newFile) {
try {
FileWriter writer = new FileWriter(newFile);
if (exportHelper(writer, roster.root)==true) {
writer.flush();
return true;
}
} catch (IOException e) {
System.out.println("An error occurred.");
return false;
}
return false;
}
private static boolean exportHelper(FileWriter writer, RedBlackTree.Node<Student> current) {
if (current == null) {
return true;
}
Student node = current.data;
String data = node.getStudentName() + ", " + ((Integer)node.getStudentId()).toString() + ", " + ((Double)node.getGrade()).toString() + "/n";
try {
writer.write(data);
} catch (IOException e) {
return false;
}
return exportHelper(writer, current.leftChild) && exportHelper(writer, current.rightChild);
}
}