-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabOne.java
46 lines (38 loc) · 1.17 KB
/
LabOne.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
package unit11.mySolutions;
//Reads numbers from keyboard and writes integers into file
import java.io.*;
public class LabOne {
public static void main(String [] args) throws IOException {
BufferedReader in = null;
DataOutputStream out = null;
int num;
String s;
if(args.length != 1) {
System.err.println("Output file name needed");
System.exit(1);
}
in = new BufferedReader(new InputStreamReader(System.in));
try {
out = new DataOutputStream(new FileOutputStream(args[0]));
}
catch(IOException e) {
System.err.println("Cannot open output file: " + args[0]);
System.exit(2);
}
// Enter ^Z or ^D for EOF
for(System.out.print("Enter integer: ");
(s = in.readLine()) != null;
System.out.print("Enter integer: ")) {
try {
num = Integer.parseInt(s);
}
catch(NumberFormatException e) {
System.err.println("Enter integers only\n");
continue;
}
out.writeInt(num);
}
in.close();
out.close();
}
}