hi,
I'm trying to write simple culaction programe using stack .. the GUI is simple one textField and one textBox for the outbut and one button
this is what I wrote in the button I tried the first operation "-" and it work but I got stuck doing the other operations
String input = txtInput.getText();
Stack stack = new Stack();
int temp = 0;
Scanner sc = new Scanner(input);
while (sc.hasNext()) {
String token = sc.next();
if (token.matches("-?\\d*")) {
if (token.matches("\\d*")) {
int num = Integer.parseInt(token);
stack.push(num);
} else if (token.matches("-?")) {
int num1 = (int) stack.pop();
int num2 = (int) stack.pop();
int total = num2 - num1;
temp = total;
stack.push(total);
txtOutput.append(stack.peek() + "\n");
} else if (token.matches("+?")) {
} else if (token.matches("*?")) {
} else if (token.matches("/?")) {
}
}
}