-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOHandler.java
53 lines (45 loc) · 1.17 KB
/
IOHandler.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
import java.io.*;
import java.util.*;
public class IOHandler {
public static void main(String[] args) {
if (args.length!=2) {
System.err.println("Incorrect Usage");
}
try {
String input = args[0];
String output = args[1];
File in = new File(input);
Scanner scanner = new Scanner(in);
File of = new File(output);
PrintWriter pw = new PrintWriter(of);
int prevTransId, transId, itemId;
prevTransId= 0;
StringBuilder sb = new StringBuilder();
while(scanner.hasNext()) {
if (!scanner.hasNext()) {
break;
}
transId = scanner.nextInt();
itemId = scanner.nextInt();
if (prevTransId==transId) {
sb.append(itemId);
sb.append(',');
} else {
sb.setLength(sb.length()-1);
pw.println(sb.toString());
sb.setLength(0);
prevTransId=transId;
sb.append(itemId);
sb.append(',');
}
}
sb.setLength(sb.length()-1);
pw.println(sb.toString());
scanner.close();
pw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}