forked from jjfiv/CSC262P1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHead.java
44 lines (37 loc) · 1.07 KB
/
Head.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
//code to remove brackets and commas : https://stackoverflow.com/questions/4389480/print-array-without-brackets-and-commas
package edu.smith.cs.csc262.coopsh.apps;
import java.util.ArrayList;
import edu.smith.cs.csc262.coopsh.InputLine;
import edu.smith.cs.csc262.coopsh.ShellEnvironment;
import edu.smith.cs.csc262.coopsh.Task;
public class Head extends Task {
//arraylist of inputlines to be printed
ArrayList<String> inputLines = new ArrayList<String>();
//lineCount
int lineCount;
public Head(ShellEnvironment env, String[] args) {
super(env, args);
}
@Override
protected void update() {
int numLines = Integer.parseInt(args[0]);
InputLine line = this.input.poll();
if (line == null) {
// still waiting for more...
return;
}
// we now have all our lines!
if (lineCount==numLines) {
//loop through list to print it
for(String str : inputLines){
this.println(str);
}
this.closeOutput();
this.exit(0);
return;
}
// Otherwise, add line to lineCount and increment this count!
inputLines.add(line.get());
lineCount++;
}
}