-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathStudentList.java
95 lines (93 loc) · 2.59 KB
/
StudentList.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
import java.io.*;
import java.text.*;
import java.util.*;
public class StudentList {
public static void main(String[] args) {
// Check arguments
if(args[0].equals("a")) {
System.out.println("Loading data ...");
try {
BufferedReader s = new BufferedReader(
new InputStreamReader(
new FileInputStream("students.txt")));
String r = s.readLine();
String i[] = r.split(",");
for(String j : i) { System.out.println(j); }
} catch (Exception e){}
System.out.println("Data Loaded.");
}
else if(args[0].equals("r"))
{
System.out.println("Loading data ...");
try {
BufferedReader s = new BufferedReader(
new InputStreamReader(
new FileInputStream("students.txt")));
String r = s.readLine();
System.out.println(r);
String i[] = r.split(",");
Random x = new Random();
int y = x.nextInt();
System.out.println(i[y]);
} catch (Exception e){}
System.out.println("Data Loaded.");
}
else if(args[0].contains("+")){
System.out.println("Loading data ...");
try {
BufferedWriter s = new BufferedWriter(
new FileWriter("students.txt", true));
String t = args[0].substring(1);
Date d = new Date();
String df = "dd/mm/yyyy-hh:mm:ss a";
DateFormat dateFormat = new SimpleDateFormat(df);
String fd= dateFormat.format(d);
s.write(", "+t+"\nList last updated on "+fd);
s.close();
} catch (Exception e){}
System.out.println("Data Loaded.");
}
else if(args[0].contains("?"))
{
System.out.println("Loading data ...");
try {
BufferedReader s = new BufferedReader(
new InputStreamReader(
new FileInputStream("students.txt")));
String r = s.readLine();
String i[] = r.split(",");
boolean done = false;
String t = args[0].substring(1);
for(int idx = 0; idx<i.length && !done; idx++) {
if(i[idx].equals(t)) {
System.out.println("We found it!");
done=true;
}
}
} catch (Exception e){}
System.out.println("Data Loaded.");
}
else if(args[0].contains("c"))
{
System.out.println("Loading data ...");
try {
BufferedReader s = new BufferedReader(
new InputStreamReader(
new FileInputStream("students.txt")));
String D = s.readLine();
char a[] = D.toCharArray();
boolean in_word = false;
int count=0;
for(char c:a) {
if(c ==' ')
{
if (!in_word) { count++; in_word =true; }
else { in_word=false;}
}
}
System.out.println(count +" word(s) found " + a.length);
} catch (Exception e){}
System.out.println("Data Loaded.");
}
}
}