-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum.java
More file actions
131 lines (100 loc) · 4.61 KB
/
sum.java
File metadata and controls
131 lines (100 loc) · 4.61 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.Scanner;
/*
|-----------------------------------|
|------------Basic Shell------------|
|-------------sum.java--------------|
|-----------------------------------|
|>> Role : Performs an addition of |
| two provided integer values |
|>> Shell Command : 'sum'. |
|>> Arguments : Two integer Values. |
|>> Pipe Support : Input Only |
\-----------------------------------/
*/
class sum {
// Main Function
public static void main(String args[]) {
// Argument passed by the shell is either 'nopipe' or 'pipeout'
// If 'nopipe' : No arguments needed when launching the program. The user inputs the two integer values when prompted by the program.
// If 'pipein' : Arguments come from the output of another program. The two integer values are read from stdout.
if (args[0].equals("nopipe")) {
// Prompt the user to enter two integers, check the validity of the user input, and print out the result of their addition
int v1 = 0;
int v2 = 0;
boolean v1_correct = false;
boolean v2_correct = false;
while (!v1_correct) {
System.out.println("Input first Value");
String val1 = System.console().readLine();
if (val1.matches("^[+-]?\\d+$")) {
v1_correct = true;
v1 = Integer.valueOf(val1);
} else {
System.out.println("Not a number!\n");
}
}
while (!v2_correct) {
System.out.println("Input second Value");
String val2 = System.console().readLine();
if (val2.matches("^[+-]?\\d+$")) {
v2_correct = true;
v2 = Integer.valueOf(val2);
} else {
System.out.println("Not a number!\n");
}
}
System.out.println(String.valueOf(v1) + " + " + String.valueOf(v2) + " = " + String.valueOf(v1 + v2));
} else if (args[0].equals("pipein")) {
try {
// Read from stdout, convert strings to the corresponding integer values, and print out the result of their addition
// !!! WARNING !! : It seems like this method only supports integer values which are in the 0-9 range. Any input taking more than one-digit will cause issues.
// The 'generate' Java program only returns values in the [0,9] range to support this
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String mvalue = in .readLine();
int x = Integer.valueOf(mvalue.charAt(0)) - 48;
int y = Integer.valueOf(mvalue.charAt(2)) - 48;
System.out.println(String.valueOf(x) + " + " + String.valueOf(y) + " = " + String.valueOf(x + y));
} catch (Exception e) {
e.printStackTrace();
}
} else if (args[0].equals("pipeout")) {
// Prompt the user to enter two integers, check the validity of the user input, and print out the result of their addition while also sending it to stdout
int v1 = 0;
int v2 = 0;
boolean v1_correct = false;
boolean v2_correct = false;
while (!v1_correct) {
System.out.println("Input first Value");
String val1 = System.console().readLine();
if (val1.matches("^[+-]?\\d+$")) {
v1_correct = true;
v1 = Integer.valueOf(val1);
} else {
System.out.println("Not a number!\n");
}
}
while (!v2_correct) {
System.out.println("Input second Value");
String val2 = System.console().readLine();
if (val2.matches("^[+-]?\\d+$")) {
v2_correct = true;
v2 = Integer.valueOf(val2);
} else {
System.out.println("Not a number!\n");
}
}
System.out.println(String.valueOf(v1) + " + " + String.valueOf(v2) + " = " + String.valueOf(v1 + v2));
try {
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
log.write(String.valueOf(v1 + v2));
log.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}