Question: How do i output the sum after each postfix notation? Every time i run the program i get Stack Exception error. Here is my code

How do i output the sum after each postfix notation? Every time i run the program i get Stack Exception error.

Here is my code below:

----------------------------------------------------

--test class-

import java.util.*;

import java.io.*;

public class Main {

public static void main(String args[]) throws IOException //driver program

{

FileOutputStream toFile=null;

Scanner fromFile=null;

PrintStream ps=null;

fromFile = new Scanner(new File("lab.txt")); //Reads from lab.txt file

toFile= new FileOutputStream("output.txt"); //outputs to the output.txt file

ps=new PrintStream(toFile); //prints output from lab.txt to output.txt

Stacks stack = new Stacks(); //Instantiate stack object from stack class

String token=" "; //string token to hold input from lab.txt

while(fromFile.hasNext()) //keep reading until end of file

{

token=fromFile.nextLine(); //stores string into token variable

System.out.println(stack.infixToPostfix(token)+" = "+ stack.sumPostfix(token));

//ps.println(stack.infixToPostfix(token));

}//end of loop

fromFile.close(); //close files

toFile.close();

}

}//end of program

------------------------------------------------

-stack class--

import java.util.Stack;

//Stack class that utilizes all methods to properly run a stack object

public class Stacks implements StackInterface{

private Node top; //head node

class Node{ //Node class

private char item; //item variable of type character to store input from lab.txt

private Node next; //moves to next node in stack

Node(char newItem) //constructor that takes one parameter of type char

{

item=newItem; //assigns value of incoming parameter to item

next=null;

}

Node(char newItem, Node nextNode) //constructor takes two parameters a char and a node

{

item=newItem; //assigns value of incoming parameter to item

next=nextNode;

}

}//end node class

Stacks(){ //Stacks class Constructor

top=null; //Assigns the Node variable top to null

}

@Override

public boolean isEmpty() { //override method that checks to see if the stack is empty

// TODO Auto-generated method stub

return top==null; //returns true if it is empty otherwise returns false

}

@Override

public void popAll() { //removes all items from the stack

// TODO Auto-generated method stub

top=null;

}

@Override

public void push(char newItem) { //push inserts a new item to the top of the stack and takes the parameter of type char

// TODO Auto-generated method stub

top= new Node(newItem, top); //assigns new item to top node

}

@Override

public char pop() throws StackException{ //removes one element from the top of the stack

if(!isEmpty()) { //if stack is not empty

//the node added most recently is popped or removed from the stack

Node temp=top;

top=top.next;

return (char) temp.item; //return the temp node value

}

else //if stack is empty return exception

{

throw new StackException("StackException on"

+ "pop: Stack empty");

}

}

@Override

public char peek() throws StackException { //retrieves top of the stack

if(!isEmpty()) //if the stack is not empty

{

return (char) top.item; //the item that was received most recently is returned however,

//the stack remains unchanged

}

else

{

throw new StackException("StackException on" //if the stack is empty then an empty stack exception is thrown

+"peek:stack empty");

}

}

public int precedence(char token) //function that will return the precedence of a given operator

//within the lab.txt expression

{ //the higher the return value the higher the precedence of the operator

switch(token) //switch statement that takes char token : PEMDAS to decide precedence

{

case '+':

case '-':

return 1; //low precedence

case'*':

case '/':

return 2;

case'^':

return 3; //highest precedence

}

return -1;

}//end

public String infixToPostfix(String expression) //converts infix expressions to postfix and takes a parameter of type String

{

String result= new String(""); //String result to hold incoming data from lab.txt

Stacks stack = new Stacks(); //create stack object from stacks class

for(int x=0;x

{

char c=expression.charAt(x);

if(Character.isLetterOrDigit(c)) //if the scanned char is an operand add it to result

{

result+=c+" ";

}

else if(c=='(') //if scanned char is a '(' push it to the stack

{

stack.push(c);

}

else if (c==')') // If the scanned character is an ')', pop and output from the stack

{

while(!stack.isEmpty() && stack.peek() != '(') { //if stack is not empty and '(' in the stack

//add expression to result

result+=stack.pop();

}

if(!stack.isEmpty() && stack.peek()!='(')

{

return "Invalid Expression"; //invalid expression in the string

}

else

{

stack.pop();

}

}

else //an operator is encountered

{

while( !stack.isEmpty() && precedence(c)<=precedence(stack.peek())) //while stack is not empty and the string in precedence is less than

//or equal to the value of thats at the top of the stack

{

result+=stack.pop(); //then add operator to result

}

stack.push(c); //add Element to the stack

}

}

while(!stack.isEmpty())

{

result+=" "+stack.pop(); //pop all the operators from the stack

}

return result; //return the String result

}

public int sumPostfix(String expression)

{

Stack stacks = new Stack<>();

for(int i=0; i

{

char c= expression.charAt(i);

if(Character.isDigit(c))

{

stacks.push(c - '0');

}

else

{

int val1 = stacks.pop();

int val2=stacks.pop();

switch(c)

{

case '+':

stacks.push(val2+val1);

break;

case '-':

stacks.push(val2- val1);

break;

case '/':

stacks.push(val2/val1);

break;

case '*':

stacks.push(val2*val1);

break;

}

}

}

return stacks.pop();

}

}

----------------------------------------------------------------------------

--input file: lab.txt---

5

5-3

5*(3+4)

7*3+5*6

2+5*(7+8)/3

(5)

3+4*5+6

9-4*(5-3)

1*2*3*4*5*6*7

(2+4)*(8-6)

3+4

9-1-2-3

2*(((4+2)))

(((((5)))))

3*7-4*2+5*6

3*5

(3*(9-7*1))/2+5

6+9*(5*(3+4))

1+2+3+4+5

9/3+8/2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!