Ok, I got the assignment from my CompSci teacher to write a program that converts a string of an infix operation to a postfix one and it has to use a list and stack. The class that we have to write, Postfix, has to include a conversion, parsing, clear, and reverse methods. I have 3 other classes a ListReferenceBaseCSCE1040, StackReferenceBaseCSCE1040, and Node that he gave us earlier in the year and then a MyTest class that actually gives something to convert and displays the results. I wrote everything down and started coding the Postfix class, and I was sure I had it, until I compiled it, which usually happens, except here I have no idea how to fix whats wrong mainly because out of 3 programs, they all say something different is wrong about the same thing.

I have 2 problems:
1) In lines 5 and 7 where I try to create a list called token and a stack called aStack, it apparently is right even though this is what I was taught.

2) In the parsing method, where it looks at the string at a specified character, it says that I can't use input.charAt(x) because they are incompatible types. And it also says that when I use character.isDigit(input.charAt(x)) it can't find the variable character. What I was told by the professor is that putting character infront converts it to a basic type.

I'm posting the code below. I would be grateful for any help you guys could give. Thanks

class Postfix {

    private ListReferenceBaseCSCE1040 token; //THIS IS WHERE IT GETS SQUIRRLY FOR THE FIRST PART
    private String output;
    private StackReferenceBaseCSCE1040 aStack;


    public Postfix(){

        token = new ListReferenceBaseCSCE1040();
        aStack = new StackReferenceBaseCSCE1040();
        output = "";

    }//end constructor postfix


        //this takes token and converts it to postfix
    public void conversion(){

        for(int i = 1; i < token.size(); i++){

            if( token.get(i).equals("(")){
                aStack.push(token.get(i));
            }else if(token.get(i).equals(")")){
                while(!aStack.peek().equals("(")){
                    output += aStack.pop();
                } //end while
            }else if((token.get(i).equals("*")) || (token.get(i).equals("/")) || (token.get(i).equals("+")) || (token.get(i).equals("-"))){
                while(!aStack.isEmpty() && precedence(token.get(i)) <= precedence(aStack.peek())){
                    output = output + " " + aStack.pop();
                } //end while
                aStack.push(token.get(i));
            }else{
                output = output + token.get(i);
            } //end ifs

        } //end for

        while(!aStack.isEmpty()){
            output = output + aStack.pop();
        } // end while

        System.out.println("Conversion of Infix to Postfix");
        System.out.println("Input: " + input);
        System.out.println("Output: " + output);

    } //end conversion


        // sets the precedence of the operators
    public int precedence(Object op){

        if((op.equals("*")) || (op.equals("/"))){
            return 2;
        }else if((op.equals("+")) || (op.equals("-"))){
            return 1;
        }else{ //parenthesis
            return 0;
        } //end ifs

    } //end precedence


    public void parsing(String input){

        String aToken = "";
        int numToken = 0;
        int i = 0;

        while( i < input.length() ){

// THIS IS WHERE IT GET FREAKY FOR THE SECOND PART
            if((input.charAt(i) == "(") || (input.charAt(i) == ")") || (input.charAt(i) == "*") || (input.charAt(i) == "/") || (input.charAt(i) == "+") || (input.charAt(i) == "-")){
                aToken = aToken + input.charAt(i);
                numToken++;
                token.add(numToken, aToken);
                aToken = "";
            }else if(character.isLetter(input.charAt(i))){
                aToken += input.charAt(i);

                while((i+1<input.length()) && (character.isLetter(input.charAt(i+1))) || (character.isDigit(input.charAt(i+1)))){
                    i++;
                    aToken += input.charAt(i);
                } //end while
                numToken++;
                token.add(numToken, aToken);
                aToken = "";
            }else if(character.isDigit(input.charAt(i))){
                aToken += input.charAt(i);
                while((i+1 < input.length()) && (character.isDigit(input.charAt(i+1)))){
                    i++;
                    aToken += input.charAt(i);
                } // end while
                numToken++;
                token.add(numToken, aToken);
                aToken = "";
            }else{
                //parenthesis
            }// end ifs

            i++; //adds to the main while loop
        } // end while

    } // end parsing


        public void clear(){
            token = new ListReferenceBaseCSCE1040();
            aStack = new StackReferenceBaseCSCE1040();
            output = "";
        }
}//end postfix

Ok, I got the assignment from my CompSci teacher to write a program that converts a string of an infix operation to a postfix one and it has to use a list and stack. The class that we have to write, Postfix, has to include a conversion, parsing, clear, and reverse methods. I have 3 other classes a ListReferenceBaseCSCE1040, StackReferenceBaseCSCE1040, and Node that he gave us earlier in the year and then a MyTest class that actually gives something to convert and displays the results. I wrote everything down and started coding the Postfix class, and I was sure I had it, until I compiled it, which usually happens, except here I have no idea how to fix whats wrong mainly because out of 3 programs, they all say something different is wrong about the same thing.

I have 2 problems:
1) In lines 5 and 7 where I try to create a list called token and a stack called aStack, it apparently is right even though this is what I was taught.

2) In the parsing method, where it looks at the string at a specified character, it says that I can't use input.charAt(x) because they are incompatible types. And it also says that when I use character.isDigit(input.charAt(x)) it can't find the variable character. What I was told by the professor is that putting character infront converts it to a basic type.

I'm posting the code below. I would be grateful for any help you guys could give. Thanks

class Postfix {

    private ListReferenceBaseCSCE1040 token; //THIS IS WHERE IT GETS SQUIRRLY FOR THE FIRST PART
    private String output;
    private StackReferenceBaseCSCE1040 aStack;


    public Postfix(){

        token = new ListReferenceBaseCSCE1040();
        aStack = new StackReferenceBaseCSCE1040();
        output = "";

    }//end constructor postfix


        //this takes token and converts it to postfix
    public void conversion(){

        for(int i = 1; i < token.size(); i++){

            if( token.get(i).equals("(")){
                aStack.push(token.get(i));
            }else if(token.get(i).equals(")")){
                while(!aStack.peek().equals("(")){
                    output += aStack.pop();
                } //end while
            }else if((token.get(i).equals("*")) || (token.get(i).equals("/")) || (token.get(i).equals("+")) || (token.get(i).equals("-"))){
                while(!aStack.isEmpty() && precedence(token.get(i)) <= precedence(aStack.peek())){
                    output = output + " " + aStack.pop();
                } //end while
                aStack.push(token.get(i));
            }else{
                output = output + token.get(i);
            } //end ifs

        } //end for

        while(!aStack.isEmpty()){
            output = output + aStack.pop();
        } // end while

        System.out.println("Conversion of Infix to Postfix");
        System.out.println("Input: " + input);
        System.out.println("Output: " + output);

    } //end conversion


        // sets the precedence of the operators
    public int precedence(Object op){

        if((op.equals("*")) || (op.equals("/"))){
            return 2;
        }else if((op.equals("+")) || (op.equals("-"))){
            return 1;
        }else{ //parenthesis
            return 0;
        } //end ifs

    } //end precedence


    public void parsing(String input){

        String aToken = "";
        int numToken = 0;
        int i = 0;

        while( i < input.length() ){

// THIS IS WHERE IT GET FREAKY FOR THE SECOND PART
            if((input.charAt(i) == "(") || (input.charAt(i) == ")") || (input.charAt(i) == "*") || (input.charAt(i) == "/") || (input.charAt(i) == "+") || (input.charAt(i) == "-")){
                aToken = aToken + input.charAt(i);
                numToken++;
                token.add(numToken, aToken);
                aToken = "";
            }else if(character.isLetter(input.charAt(i))){
                aToken += input.charAt(i);

                while((i+1<input.length()) && (character.isLetter(input.charAt(i+1))) || (character.isDigit(input.charAt(i+1)))){
                    i++;
                    aToken += input.charAt(i);
                } //end while
                numToken++;
                token.add(numToken, aToken);
                aToken = "";
            }else if(character.isDigit(input.charAt(i))){
                aToken += input.charAt(i);
                while((i+1 < input.length()) && (character.isDigit(input.charAt(i+1)))){
                    i++;
                    aToken += input.charAt(i);
                } // end while
                numToken++;
                token.add(numToken, aToken);
                aToken = "";
            }else{
                //parenthesis
            }// end ifs

            i++; //adds to the main while loop
        } // end while

    } // end parsing


        public void clear(){
            token = new ListReferenceBaseCSCE1040();
            aStack = new StackReferenceBaseCSCE1040();
            output = "";
        }
}//end postfix

end quote.

I don't understand what the problem is for part 1:

1) In lines 5 and 7 where I try to create a list called token and a stack called aStack, it apparently is right even though this is what I was taught.

I'm guessing there is a typo in there? The word "right" should be "wrong"? Also, what is the error that you are getting?

For part 2, a few things. One, try changing the "character" to "Character" and see if the error goes away. Two, in line 74, try changing the double quotes to single quotes. Characters are denoted by single quotes. That may be the mismatch.

yes, sorry. it should be wrong. I feel like an idiot. I have now fixed the first problem.

Thank you for your input on the second problem. That fixed it. I hate having a korean for a teacher cause everything he writes in english looks alike, so I guess I must have missed the single quote double quote thing.

thank you i want to be good in my field and i need some help i am a student my coarse is BSIT first year 2nd sem thank you

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.