Question: My code compiles but wont run. My program is suppose to accept a postfix expression. I've been trying to fix but it wont work. Can
My code compiles but wont run. My program is suppose to accept a postfix expression. I've been trying to fix but it wont work. Can anyone help fix it?
I get the following error message when I try to run it:
" Static Error: This class does not have a static void main method accepting String[]."
Below is my code:
import java.io.*; import java.util.Scanner;
class myStack
{ private int maxSize; private String[] stackArray; private int top; public myStack(int size) // constructor of stack class { maxSize = size; stackArray = new String[maxSize]; top = -1; } public int push(String j) // put item on top of stack { if (top == maxSize){ System.out.println("Stack full. Terminating!"); return -1; } System.out.println("push " + j); stackArray[++top] = j; return 0; } public String pop() // take item from top of stack { if (top == -1){ System.out.println("No operands! Empty stack!"); return ""; } return stackArray[top--]; }
} class postfix { private static myStack theStack; private static BufferedWriter bw;
public static void doParse(String input) throws Exception { theStack = new myStack(50); // make new stack char ch; int j; String operand1, operand2, interAns; interAns= ""; int n = 1;
for(j=0; j { ch = input.charAt(j); // read from input if(ch >= 'A' && ch <= 'Z'){ // if it's a operand int correct = theStack.push( String.valueOf(ch) ); // push inserts an element to the stack if (correct == -1) return; } else // if it is an operator { operand2 = theStack.pop(); operand1 = theStack.pop(); if (operand1 == "" || operand2 == ""){ System.out.println("Invalid Expression!!!"); // only one operand in stack; stop bw.write(" Invalid Expression!!!"); bw.flush(); return; } bw.write(" LD " + operand1); switch(ch) // do arithmetic { case '+': // addition System.out.println("AD " + operand2); bw.write(" AD " + operand2); interAns = "TEMP" + String.valueOf(n); n++; break; case '-': // Subtraction System.out.println("SB " + operand2); bw.write(" SB " + operand2); interAns = "TEMP" + String.valueOf(n); n++; break; case '*': // Multiplication System.out.println("ML " + operand2); bw.write(" ML " + operand2); interAns = "TEMP" + String.valueOf(n); n++; break; case '/': // Division System.out.println("DV " + operand2); bw.write(" DV " + operand2); interAns = "TEMP" + String.valueOf(n); n++; break; case '$': // Exponentiation System.out.println("EXP " + operand2); bw.write(" EXP " + operand2); interAns = "TEMP" + String.valueOf(n); n++; break; default: System.out.println("Invalid token: " + ch + "!!!"); bw.write(" Invalid token: " + ch + "!!!"); bw.flush(); return; } // end switch System.out.println("ST " + interAns); bw.write(" ST " + interAns); theStack.push(interAns); // push result } // end else } // end for bw.flush(); } // end doParse()
public static void main(String[] args) throws IOException { String input; int output; System.out.println("Please enter file input name:"); Scanner scanner = new Scanner(System.in);
BufferedReader br = new BufferedReader(new FileReader(scanner.next())); System.out.println("Please enter output file name:"); bw = new BufferedWriter(new FileWriter(scanner.next()));
while((input = br.readLine())!=null){ // for each line of input postfix expression System.out.println("========================"); System.out.println("Input = " + input); System.out.println("Output = "); bw.write(" ========================"); bw.write(" Input = " + input); bw.write(" Output = "); try{ doParse(input.trim()); // do the evaluation for each line in the input } catch(Exception e){ System.out.println("IO exception"); } } bw.flush(); bw.close(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
