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 3 of 3

Thread: Error of "cannot access InToPost" in 3 and 5 code

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Error of "cannot access InToPost" in 3 and 5 code

    hey guys.. howdy?

    i am trying to compile my program however i'm having a hard time. it says there's an error. im a newbie so imma post it here the codes... please help me if you can ^^

    import java.io.*;
    import java.util.Stack;
    import java.io.IOException;
     
    /** 
    *Check if a string is a palindrome
    *
    */
     
    public class Palindrome{
        private Stack theStack;
        private String input;
          private String output = "";
     
          public Palindrome(String in) {
            input = in;
            int stackSize = input.length();
            theStack = new Stack();
          }
     
        public String isPalindrome(){
            boolean chk = false;
            int midpoint = input.length() / 2;
            if(input.length() % 2 == 1){    
                midpoint = input.length() / 2 + 1;
            }
     
            for(int i=0; i < input.length() / 2; i++){
                    char ch = input.charAt(i);        
                    theStack.push(ch);        
                    System.out.println(input.charAt(i));                        
            }        
     
            for(int j=midpoint; j < input.length(); j++){
                System.out.println(input.charAt(j));    
                if(!theStack.empty() && input.charAt(j) == theStack.peek()){
                    theStack.pop();
                    chk = true;
                }else{
                    chk = false;
                }                        
            }
            if(chk){
                output = "The String is a palindrome";
            }else{
                output = "The String is not a palindrome";
            }    
     
            //System.out.println(midpoint);
            return output;
        }
     
        public static void main(String[] args) throws Exception{
            BufferedReader sr = new BufferedReader(new InputStreamReader(System.in));
            String output;
                System.out.print("Please enter a string: ");
                String inputPalindrome = sr.readLine();
     
            Palindrome theTrans = new Palindrome(inputPalindrome);
            output = theTrans.isPalindrome();
            System.out.println(output);    
     
        }
     
    }

    import java.util.*;
    import java.io.IOException;
     
    /** 
    *Evaluates a postfix expression 
    *
    */
     
    public class evalPostfix{
     
        private static final String operators = "+-*/ ";
            private int operate(String op, int op1, int op2){
                    if (op.equals("+")) {
                    return op1 + op2;
                    }
                    if (op.equals("*")){
                    return op1 * op2;
                    }
                    if (op.equals("-")){
                    return op1 - op2;
                    }
                    if (op.equals("/")){
                    return op1 / op2;
                    }
                throw new IllegalArgumentException("unrecognized operator "+op);
            }
     
        public int evaluate(String s){
            Stack stack = new Stack();
            StringTokenizer tokens = new StringTokenizer(s,operators,true);
                while (tokens.hasMoreTokens()){
                    String t = tokens.nextToken();
                        if (operators.indexOf(t) >= 0){
                            if (t.equals(" ")) continue;
                        int right = Integer.parseInt((String) stack.pop());
                        int left = Integer.parseInt((String) stack.pop());
                        stack.push(""+operate(t,left,right));
                        }
                    else {
                    stack.push(t);
                    }
                }
            int result = Integer.parseInt((String) stack.pop());
            if (stack.size() > 0){
            throw new IllegalArgumentException("non empty stack on "+s);
            }
            return result;
        }
     
    public static void main(String[] args){
        String expression = " 25 10 * 50 + 50 - 50 /";
            if (args.length > 0){
                expression = args[0];
            }
        evalPostfix p = new evalPostfix();
        int result = p.evaluate(expression);
        System.out.println("value of "+expression+" = "+result);
        }
    }

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
     
     
    public class GUI2 extends JFrame implements  KeyListener {
        JButton buttonAccept;
        JTextField textLetter, textFix;
     
     
        JList listComponentProduct;
        DefaultListModel listModelProduct;
     
        public GUI2() {
            createGUI();
        }
     
        public void createGUI() {
            JPanel panel, panelSearch;
            JLabel mainLabel, labelColumnHeader, labelA, labelB, labelC, labelD, labelE, labelF, labelG, labelH;
     
     
            Font f = new Font("Cambria", Font.PLAIN, 18);
            GridBagConstraints gbc = new GridBagConstraints();
     
            listModelProduct = new DefaultListModel();
     
            listComponentProduct = new JList(listModelProduct);
            listComponentProduct.setFont(f);
     
            mainLabel = new JLabel(" M E N U ");
            mainLabel.setFont(f);
            labelA = new JLabel("");
            labelB = new JLabel("A. Evaluate a postfix expression.                   ");
            labelC = new JLabel("B. Convert from infix to postfix expression.     ");
            labelD = new JLabel("C. Check if a string is a palindrome.               ");
            labelE = new JLabel("D. Quit                                               ");
            labelF = new JLabel("Letter of your choice: ");
     
     
            buttonAccept = new JButton("Accept");
     
     
            textLetter = new JTextField(1);
            textLetter.setEnabled(true);
            textLetter.addKeyListener(this);
            textLetter.setFont(f);
     
            panelSearch = createSearchPanel();
     
            panel = new JPanel();
            panel.setLayout(new GridBagLayout());
     
            gbc.insets = new Insets(2, 2, 2, 2);
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.fill = GridBagConstraints.HORIZONTAL;
     
            gbc.gridx=0; gbc.gridy=0; panel.add(mainLabel, gbc);
     
            gbc.gridx=0; gbc.gridy=8; panel.add(textLetter, gbc);
            gbc.gridx=0; gbc.gridy=6; panel.add(new JSeparator(), gbc);
            gbc.gridx=0; gbc.gridy=1; panel.add(labelA, gbc);
            gbc.gridx=0; gbc.gridy=2; panel.add(labelB, gbc);
            gbc.gridx=0; gbc.gridy=3; panel.add(labelC, gbc);
            gbc.gridx=0; gbc.gridy=4; panel.add(labelD, gbc);
            gbc.gridx=0; gbc.gridy=5; panel.add(labelE, gbc);
            gbc.gridx=0; gbc.gridy=7; panel.add(labelF, gbc);
            gbc.gridx=0; gbc.gridy=9; panel.add(buttonAccept, gbc);
     
     
     
            setTitle("Group Exercise");
            setContentPane(panel);
            pack();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setResizable(false);
            setVisible(true);
        }
     
        public JPanel createSearchPanel() {
            JPanel panel;
            GridBagConstraints gbc = new GridBagConstraints();
     
            Font f = new Font("Lucida Console", Font.PLAIN, 22);
            ButtonGroup buttonGroup;
     
     
            panel = new JPanel();
            panel.setLayout(new GridBagLayout());
     
            gbc.insets = new Insets(2,2,2,2);
            gbc.anchor = GridBagConstraints.WEST;
     
            gbc.gridx=1; gbc.gridy=10; panel.add(buttonAccept, gbc);
            return panel;
        }
        public void LetterB(){
            BufferedReader sr = new BufferedReader(new InputStreamReader(System.in));    
            try{ 
                System.out.println("Please enter an infix expression to convert to postfix. Separate the terms by a space: ");
                                String inputInfix = sr.readLine();
                            String outputPostfix;
                             InToPost infixTrans = new InToPost(inputInfix);
                            outputPostfix = infixTrans.doTrans(); 
                            System.out.println("Postfix is " + outputPostfix + '\n');
                            } catch(Exception e){
                e.printStackTrace();
            }
     
            }
     
        public void LetterC(){
            BufferedReader sr = new BufferedReader(new InputStreamReader(System.in));    
            try {
                    System.out.print("Please enter a string: ");
                            String inputPalindrome = sr.readLine();
                            String outputPalindrome;
                            Palindrome chkString = new Palindrome(inputPalindrome);
                            outputPalindrome = chkString.isPalindrome();
                            System.out.println(outputPalindrome);
                            } catch(Exception e){
                e.printStackTrace();
            }        
            }
        public void LetterD(){
            System.exit(0);    
        }
        public void keyPressed(KeyEvent ke) {}
        public void keyTyped(KeyEvent ke) {}
        public void keyReleased(KeyEvent ke) {
            }
     
        // ActionListener
     
     
     
        public static void main(String[] args) {
            new GUI2();
        }}

    package GUI4;
     
    import java.io.IOException;
     
    /** 
    *Converts a infix expression to postfix 
    *
    */
     
    public class InToPost {
      private Stack theStack;
     
      private String input;
     
      private String output = "";
     
      public InToPost(String in) {
        input = in;
        int stackSize = input.length();
        theStack = new Stack(stackSize);
      }
     
      public String doTrans() {
        for (int j = 0; j < input.length(); j++) {
          char ch = input.charAt(j);
          switch (ch) {
          case '+': 
          case '-':
            gotOper(ch, 1); 
            break; //   (precedence 1)
          case '*': // it's * or /
          case '/':
            gotOper(ch, 2); // go pop operators
            break; //   (precedence 2)
          case '(': // it's a left paren
            theStack.push(ch); // push it
            break;
          case ')': // it's a right paren
            gotParen(ch); // go pop operators
            break;
          default: // must be an operand
            output = output + ch; // write it to output
            break;
          }
        }
        while (!theStack.isEmpty()) {
          output = output + theStack.pop();
     
        }
        //System.out.println(input);
        return output; // return postfix
      }
     
      public void gotOper(char opThis, int prec1) {
        while (!theStack.isEmpty()) {
          char opTop = theStack.pop();
          if (opTop == '(') {
            theStack.push(opTop);
            break;
          }// it's an operator
          else {// precedence of new op
            int prec2;
            if (opTop == '+' || opTop == '-')
              prec2 = 1;
            else
              prec2 = 2;
            if (prec2 < prec1) // if prec of new op less
            { //    than prec of old
              theStack.push(opTop); // save newly-popped op
              break;
            } else
              // prec of new not less
              output = output + opTop; // than prec of old
          }
        }
        theStack.push(opThis);
      }
     
      public void gotParen(char ch){ 
        while (!theStack.isEmpty()) {
          char chx = theStack.pop();
          if (chx == '(') 
            break; 
          else
            output = output + chx; 
        }
      }
     
      public static void main(String[] args) throws IOException {
        String input = "A + B";
        String output;
        InToPost theTrans = new InToPost(input);
        output = theTrans.doTrans(); 
        System.out.println("Postfix is " + output + '\n');
     
      }
     
     
      class Stack {
        private int maxSize;
     
        private char[] stackArray;
     
        private int top;
     
        public Stack(int max) {
          maxSize = max;
          stackArray = new char[maxSize];
          top = -1;
        }
     
        public void push(char j) {
          stackArray[++top] = j;
        }
     
        public char pop() {
          return stackArray[top--];
        }
     
        public char peek() {
          return stackArray[top];
        }
     
        public boolean isEmpty() {
          return (top == -1);
        }
      }
     
    }

    import java.util.Stack;
    import java.io.*;
     
    /** 
    *This applet is a menu-driven application program that uses the Stack data structure.
    *It has the following applications: 
    *-Evaluates a postfix expression 
    *-Converts a infix expression to postfix
    *-Check if a string is a palindrome
    *
    *
    */
     
    public class MidGroupAct{
     
        public static void main(String[] args ) throws Exception {
            BufferedReader sr = new BufferedReader(new InputStreamReader(System.in));    
            char input;
            boolean quit = false;
            double y, x, z;
            Stack operands = new Stack();
     
            while (!quit) {
                System.out.println();
                System.out.println("     M  E  N  U                                   ");
                System.out.println("|---------------------------------------------|");
                System.out.println("|A. Evaluate a postfix expression.              |");
                System.out.println("|B. Convert from infix to postfix expression. |");
                System.out.println("|C. Check if a string is a palindrome.          |");
                System.out.println("|D. Quit                                       |");
                System.out.println("|---------------------------------------------|");
                System.out.println();
                System.out.print("Please enter the letter corresponding to your choice: ");
                input = sr.readLine().charAt(0);
     
     
                switch (input){
                        case 'A':
                        case 'a':
                            System.out.println("Please enter a postfix expression to evaluate. Separate the terms by a space: ");
                                String inputPostfix = sr.readLine();
                                 if (args.length > 0){
                                    inputPostfix = args[0];
                                }
                            evalPostfix p = new evalPostfix();
                            double outputEvaluated = p.evaluate(inputPostfix);
                            System.out.println("The value of the expression is " + outputEvaluated);
                            break;
     
                            case 'B':
                        case 'b':
                            System.out.println("Please enter an infix expression to convert to postfix. Separate the terms by a space: ");
                                String inputInfix = sr.readLine();
                            String outputPostfix;
                             InToPost infixTrans = new InToPost(inputInfix);
                            outputPostfix = infixTrans.doTrans(); 
                            System.out.println("Postfix is " + outputPostfix + '\n');
                            break;
     
                        case 'C':
                        case 'c':
                            System.out.print("Please enter a string: ");
                            String inputPalindrome = sr.readLine();
                            String outputPalindrome;
                            Palindrome chkString = new Palindrome(inputPalindrome);
                            outputPalindrome = chkString.isPalindrome();
                            System.out.println(outputPalindrome);
                            break;    
     
                            case 'D':
                            case 'd':    
                                System.exit(0);    
                            default: 
                                System.out.println("Invalid character. Please enter A, B, C or D");
                    }
            }
            sr.close();
        }
    }
    everything goes well except with the code 3 and 5 im having an error "cannot access InToPost"


    thanks
    Last edited by Deep_4; November 8th, 2012 at 01:17 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Need Help Stack Error

    Hey jaysoncutie,

    I attempted to compile your code and I get an error in the Palindrome class here:

    [B]if(!theStack.empty() && input.charAt(j) == theStack.peek()){[/B]
    "Incompatable operand types char and Object"

    You need to address this issue...

    When I comment out this part of the code and run the application, I do not get any errors.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need Help Stack Error

    that's crucial code to his program, javapf You're trying to compare a String with a character, and it's failing. I'd suggest doing this:
    if(!theStack.empty() && input.subString(j,j+1).equals(theStack.peek())){

    or, alternatively do this:
    if(!theStack.empty() && theStack.peek().equals((String) input.charAt(j))){

Similar Threads

  1. Error while deploying .ear file
    By urslalitha in forum Exceptions
    Replies: 1
    Last Post: August 18th, 2009, 02:56 AM
  2. Can we increase the stack size for JVM ?
    By prasanna in forum Java Theory & Questions
    Replies: 4
    Last Post: August 4th, 2009, 03:17 PM
  3. How to solve NullPointerException in runtime?
    By Koren3 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: April 2nd, 2009, 03:20 AM
  4. Proper output for Non preemptive scheduling problem
    By haygaurav in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 4th, 2009, 06:58 AM
  5. Replies: 0
    Last Post: October 14th, 2008, 06:40 PM

Tags for this Thread