The assignment is to change an input that is infix into postfix and then output it. I am supposed to implement a stack, which I have, in order to accomplish this. The final solution will have a file as an input, but for practice I've decided to try it by writing in an input first. I think I have the Stack class down, at least for now, but its the main method I'm currently having trouble with. When I try to run my program, this is the error I get from the compiler:
Exception in thread "main" java.lang.NoClassDefFoundError: javaprogram2/Stack at javaprogram2.JavaProgram2.main(JavaProgram2.java:24) Caused by: java.lang.ClassNotFoundException: javaprogram2.Stack at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
And this is my current code:
public class JavaProgram2 { /** * @param args the command line arguments */ public static void main(String[] args)throws Exception { String s; BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); Stack one = new Stack(); System.out.println("Enter input string."); s = br.readLine(); System.out.println("Input string: " + s); System.out.println("Output string: "); one.postFix(s); } }public class Stack { char stackChar[]= new char[20]; int top; public void push(char ch){ top++; stackChar[top]=ch; } public char pop(){ char ch; ch=stackChar[top]; top--; return ch; } public int pre(char ch){ switch(ch){ case'-': return 1; case'+': return 1; case'*': return 2; case'/': return 2; } return 0; } public boolean operator(char ch){ if(ch=='-' || ch=='+' || ch=='*' || ch=='/') { return true; } else { return false; } } public boolean isAlpha(char ch){ if(ch>='a' && ch<='z'|| ch>='0' && ch=='9') { return true; } else { return false; } } public void postFix(String str){ char output[] = new char[str.length()]; char ch; int t = 0; int i; int j; for(i=0; i<str.length(); i++){ ch = str.charAt(i); if(ch=='('){ push(ch); } else if(isAlpha(ch)){ output[t++]=ch; } else if(operator(ch)){ if(stackChar[top]== 0 || (pre(ch)>pre(stackChar[top]))|| stackChar[top]=='('){ push(ch); } } else if(pre(ch)<=pre(stackChar[top])){ output[t++]=pop(); push(ch); } else if(ch=='('){ while((ch=pop())!='('){ output[t++]=ch; } } } while(top!=0){ output[t++]=pop(); } for(j=0; j<str.length(); j++){ System.out.print(output[j]); } } }
I asked my teacher for help, and she recommended that I grab a skeleton online and try to fill it in with the methods for the stack, which I did. Any help with understanding whats wrong would be greatly appreciated.