Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with infix to postfix problem.

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Help with infix to postfix problem.

    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.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with infix to postfix problem.

    The exception states that the JRE cannot find the Stack class...How are you compiling and running the program, and where do the two class files for Stack and JavaProgram2 reside? Are either of your classes a part of a package (there is no package declaration, but I don't want to make any assumptions)?

Similar Threads

  1. Infix to postfix using stacks
    By kbrahmani in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 22nd, 2013, 01:54 AM
  2. [SOLVED] need help converting from infix to postfix notation
    By mia_tech in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 27th, 2012, 02:45 PM
  3. Infix calculator problem
    By cubertron in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 2nd, 2011, 07:32 AM
  4. Infix to Prefix
    By cscstudent in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 26th, 2011, 11:28 PM
  5. convert infix to postfix
    By tina G in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 11th, 2010, 01:46 AM