-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUgradRecordSystem.java
71 lines (65 loc) · 1.6 KB
/
UgradRecordSystem.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
import java.io.*;
import java.util.*;
public class UgradRecordSystem {
public static final String DELIM = "\t";
/*
* Static can call static
* Non-static can call static
* Static CANNOT call non-static
*/
public static void writeToFile(String fileName, Ugrad[] uGrads)
{
try
{
PrintWriter fileWriter = new PrintWriter(new FileOutputStream(fileName));
//<<name>>\t<<id>>\t<<level??\n
for(Ugrad u: uGrads)
{
fileWriter.println(u.getName() + DELIM + u.getId() + DELIM + u.getLevel());
}
fileWriter.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
public static Ugrad[] readFromFile(String fileName)
{
try
{
Scanner fileScanner = new Scanner(new File(fileName));
//First pass: count each endline
int count = 0;
while(fileScanner.hasNextLine())
{
fileScanner.hasNextLine();
count++;
}
Ugrad[] retU = new Ugrad[count];
//Second pass: process each line
fileScanner = new Scanner(new File(fileName)); //Reset the scanner
count = 0;
while(fileScanner.hasNextLine())
{
//Read the line
String line = fileScanner.nextLine();
//Split the line
String[] splitStr = line.split(DELIM);
if(splitStr.length != 3) //Check the line
continue;
retU[count] = new Ugrad(splitStr[0], Integer.parseInt(splitStr[1]), Integer.parseInt(splitStr[2]));
count++;
}
fileScanner.close();
}
catch(Exception e)
{
System.out.println(e);
}
return null;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}