Hi to all, I am stuck with these problem, so I must ask for a little help. I have this "postfix notation" expression " 5 9 + 2 * 6 5 * + " ,written in a file "izrazi.txt". So , I am supposed to read it from the file, and solve it. 'Cause this is the same expression as "(5 + 9) * 2 + 6 * 5". So I need to read every element, and if it's an operand (number),put it in the stack. Then if the next element is an operator, I should put the operands out, calculate it, and put the result in the stack. In the end, I should have a stack with only one element - the final result. So I made a class, I tried to solve this problem, but I'm stuck in recognizing if an element is an operand or an operator. I mean, how can I tell if it's an "int" or a string, if I'm reading only strings from the file. Thank you in advance for any help. Here is the code.
import java.util.*;
import java.io.*;
public class Stack {
private int top;
private String [] stack;
Stack (){
this.top = 0;
}
public void push(String a) throws Exception{
if(top == stack.length){
throw new Exception("stack overflow");
}
else{
stack[top] = a;
top++;
}
}
public String pop() throws Exception{
if(top == 0){
throw new Exception("stack empty");
}
else{
--top;
return stack[top];
}
}
public void getStack() {
for (int i = 0; i < stack.length; i++){
System.out.println(stack[i]);
}
}
public static void main(String [] args) throws IOException {
File fileInput = new File("izraz.txt");
BufferedReader input = new BufferedReader(new FileReader(fileInput));
String s;
while((s = input.readLine()) != null){
System.out.println(s);
}
StringTokenizer st;
st = new StringTokenizer(s);
while(st.hasMoreTokens()){
if(st.nextToken())
}
}
}