I need help with how to handle parentheses when converting an infix equation to postfix through the use of a stack.
The program reads the equations in from a .txt file.

It works for the equations without parentheses, but not for those with parentheses.

I've attached the text file to this thread and here is my code:

import java.util.Scanner;
import java.io.*;
import java.lang.Character;



public class infixToPostfix {
	String fname;
	
	//  MAIN  ////////////////////////////////////
	public static void main(String[] args) {
		System.out.println("START main method...");
		infixToPostfix test = new infixToPostfix();
		System.out.println("END main method...");
	}
	//////////////////////////////////////////////
	
	public infixToPostfix() {
		System.out.println("starting infixToPostfix() method...");
		getFileName();
		readFileContents();
	}
	
	public void getFileName() {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter file name please.");
		fname = in.nextLine();
		System.out.println("You entered " + fname);
	}
	
	public void readFileContents() {
		boolean looping;
		DataInputStream in;
		String line;
		int j, len;
		char ch;
		stackOb myStack = new stackOb(20);

		try {
			in = new DataInputStream(new FileInputStream(fname));
			looping = true;
			while (looping) {
			
				if (null == (line = in.readLine())) {
					looping = false;
					in.close();
				}
				else {
					myStack.reset();
					String postfixString = new String();
					System.out.println("infix line:    " + line);
					len = line.length();
					for (j = 0; j < len; j++) {
						if ((Character.isLetter(line.charAt(j)))||(Character.isDigit(line.charAt(j)))) {
							postfixString += line.charAt(j);
						}
						if ((line.charAt(j)=='+')||(line.charAt(j)=='-')||(line.charAt(j)=='*')) {
							if (myStack.empty()==true) {
								myStack.push(line.charAt(j));
							}
							else if (myStack.empty()==false) {
								if ((line.charAt(j)=='*')&&((myStack.peek()=='+')||(myStack.peek()=='-'))) {
									myStack.push(line.charAt(j));
								}
								else if (((line.charAt(j)=='+')||(line.charAt(j)=='-')) && (myStack.peek()=='*')) {
									postfixString += myStack.peek();
									myStack.pop();
									myStack.push(line.charAt(j));
								}
								else if (((line.charAt(j)=='+')||(line.charAt(j)=='-')) && ((myStack.peek()=='+')||(myStack.peek()=='-'))) {
									postfixString += myStack.peek();
									myStack.pop();
									myStack.push(line.charAt(j));
								}
							}
						}
					}
					while (myStack.empty()==false) {
						postfixString = postfixString + myStack.peek();
						myStack.pop();
					}
					System.out.println("postfix line:  " + postfixString);
				}// end else
			}// end while
		}// end try
		
		catch(IOException e) {
			System.out.println("Error " + e);
		} //end catch
	}
}


class stackOb {
	int top;
	char stack[];
	int maxLen;
	
	public stackOb(int maxLen) {
		stack = new char[maxLen];
		top = -1;
		maxLen = maxLen;
	}
	public char peek() {
		return stack[top];
	}
	public void push(char item) {
		top++;
		stack[top] = item;
	}
	public void pop() {
		top--;
	}
	public boolean empty() {
		if (top == -1) {
			return true;
		}
		else {
			return false;
		}
	}
	public void reset() {
		top = -1;
	}
	public void showStack() {
		int j;
		System.out.println("Stack contents ...");
		for(j=top;j>-1;j--) {
			System.out.println(stack[j]);
		}
	}
	public void showStack0toTop() {
		int j;
		System.out.println(" ");
		System.out.println("Stack contents ...");
		for(j=0;j<=top;j++) {
			System.out.print(stack[j]);
		}
		System.out.println(" ");
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.