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())
			
		}
		
		
	}

}

I think I can try with this one

if(st.nextToken() == "+" || st.nextToken() == "*"){

But it would be hard to do it...

Well, you certainly don't want to use "==" for string equality.
You could check for any of them with a regular expression.

I got this far, but I guess this doesn't help..

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 Exception {
		
		Stack stack = new Stack();
		
		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);
		String [] array = s.split(" ");
		for (int i = 0; i < array.length; i++){
		//while(st.hasMoreTokens()){
			boolean sign = false;
			if(array[i] == "+" || array[i] == "*"){
				
				sign = true;
				
				
			}
			if (sign == false){
				String m = array[i];
				stack.push(m);
				
			}
		}
		//}
		
		
	}

}

Nope, because you're still using == to check string equality instead of the equals() method.

Anyway, I'd recommend looking at this method in the Scanner API: Scanner.hasNextInt()

Optionally, you might want to consider looking at the regex Pattern class as well.

Comments
*nods*

I think i almost solved it, but I get this "FileNotFoundException", although I have this "izraz.txt" file in the correct location. Can anyone take a look at the code please ? Thanks in advance.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class Postfix {
	
	private Object [] stack;
	private int pos;

	public Postfix(int n){
		this.stack = new Object[n];
		this.pos = 0;
	}
	
	public void push(Object o) throws Exception{
		if(pos == stack.length){
			throw new Exception ("stack overflow");
		}
		else{
			stack[pos] = o;
			pos++;
		}
	}
	
	public Object pop() throws Exception{
		if(pos == 0){
			throw new Exception("stack is empty");
		}
		else{
			Object obj = stack[pos - 1];
			pos--;
			return obj;
			
		}
		
		
	}
	
	public void calculate (Object sign) throws Exception{
		int a, b;
		int result = 0;
		String string = new String();
		string += pop();
		a = Integer.parseInt(string);
		string = new String();
		string += pop();
		b = Integer.parseInt(string);
		
		
		if(sign.equals('+')){
		
			result = a + b;
			push(result);
		}
		if(sign.equals('-')){
			result = a - b;
			push(result);
			
		}
		if(sign.equals('*')){
			result = a * b;
			push(result);
			
		}if(sign.equals('/')){
			result = a / b;
			push(result);
			
		}
	}
	
	public String toString(){
		String string = null;
		for (int i = 0; i < stack.length; i++){
			if(stack[i] != null){
				string += stack[i];
			}
		}
		return string;
	}
	
	public static void main(String[] args) throws Exception {
		
		BufferedReader reader = new BufferedReader(new FileReader("izraz.txt"));
		String string = new String();
		String s = new String();
		while((string = reader.readLine()) != null){
			s += string;
		}
		Postfix p = new Postfix(s.length());
		for (int i = 0; i < s.length(); i++){
		char c = s.charAt(i);
		if (c != '+' && c != '*' && c != ' '){
			p.push(c);
		}
		else{
			if (c != ' '){
				p.pop();
			}
		}
		}
		System.out.println(p.toString());
	}
		
	
}

> although I have this "izraz.txt" file in the correct location

If that were the case, the said exception won't be thrown by the Java runtime. The solution to this issue depends on how you are executing your program. Are you using an IDE, or is it a command line invocation of `java' or are you using any build tool like Ant?

A simple way of knowing your current working directory is to create a blank file with some unique name [e.g. java-sucks.txt] using your PostFix program and search your file system for that file; the location where you file was created is where you should have your `izraz.txt'.

BTW, you still have parsing logic in main() and dynamic string creation is better done using StringBuilder than using += repeatedly inside a loop.

Well, you are right :) I just cannot see how I missed it... I had my project named different from my class, so I was looking for the class name in my Eclipse Workspace all along... So foolishly of me, but it's OK now. Thanks a lot, and may just say something - Life is great - enjoy it !

This question has already been answered. Start a new discussion instead.