Hi, I need some help on my stack i could not input the infix because it will generate error when i compile,and it says that non-static method priority (char)cannot be reference from a static context....Thank you in avdvance.

    public InfixToPostfix(int size)
    {
        stack=new Object[size];
        top=0;
    }

    public boolean isEmpty()
    {
        return top==0;
    }

    public boolean isFull()
    {
        return top==stack.length;
    }

    public boolean push(Object item)
    {
        boolean ok=!isFull();
        if(ok)
        {
            stack[top++]=item;
            ok= true;
        }

        return ok;
    }

    public Object peek()
    {
        return stack[top-1];

    }

    public Object pop()
    {
       Object item=peek();
       if(item!=null)
       {
         stack[top-1]=null;
         top--;

       }
        return item;
    }

    public int priority(char c)
    {
        int p=999;

        switch(c)
        {
            case '*': case  '/': p=0; break;
            case '+': case  '-': p=1; break;
        }
        return p;
    }


    public static void main(String...args)
    {
        InfixToPostfix s =new InfixToPostfix(5);


        Object[] Infix={0};

        Object[] postfix={0
        };

        int count=0;
        char ch='u0000';

         System.out.print("Enter infix: ");
          for(int i=0;i<s.stack.length;i++){
                Infix[i]=new java.util.Scanner(System.in).next();
         }



         for(int i=0;i<Infix.length;i++)
         {

             ch=(char)Infix[i];

            if(Character.isDigit(ch)) 
                postfix[count++]=ch;

            else
            {
              while(priority((char)s.peek())<=priority(ch))
              {
                postfix[count++]=s.pop();
              }

              s.push(ch);   
            }
         }


         while(!isEmpty())
         {
            postfix[count++]=s.pop();
         }
         postfix[count]=null;
         System.out.print(postfix);




    }
    public String toString()
    {
      StringBuffer sb=new StringBuffer();

      for(int i=top-1;i>-1;--i)
      {
        sb.append(stack[i]+",");


      }

      return sb.toString();

    }


}

Edited 4 Years Ago by JamesCherrill: Fixed title spelling

To call a non-static method defined in a class, you need a reference to an instance of the class.

for example, this line uses the reference variable: s to call the peek() method.

s.peek()

@NormR1,Thank you i fixed now but i have another problem,if i will input 1+2-3 there is no output.

Try debugging the code by adding println statements to print out the values of variables as they are changed and to show the program's execution flow as methods are called.

@Norm1, I tried to put size 1 on the

InfixToPostfix s =new InfixToPostfix(1);  

I inputted this:
Enter infix: 1+2-3      
here is the error...
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Character
at InfixToPostfix.main(InfixToPostfix.java:90)

Edited 4 Years Ago by ezekel

java.lang.String cannot be cast to java.lang.Character

The text explains the problem. A String object and a Character object are two different things.

What are you trying to do? Can you post the code with the problem?

From glancing, why does Infix has size of 1 but you attempt to use a for loop on it? That doesn't make sense...

Enter infix: 1+2+3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at InfixToPostfix.peek(InfixToPostfix.java:39)
    at InfixToPostfix.main(InfixToPostfix.java:97)





    public static void main(String...args)
    {
        InfixToPostfix s =new InfixToPostfix(1);


        Object[] Infix=new Object [1];

        Object[] postfix={0
        };

        int count=0;
        char ch='\u0000';

         System.out.print("Enter infix: ");
         for(int i=0;i<s.stack.length;i++){
                Infix[i]=new java.util.Scanner(System.in).next();
         }

         char [] cc = Infix.toString().toCharArray();
         for(int i=0;i< cc.length;i++)
         {

             ch=cc[i];

            if(Character.isDigit(ch)) 
                postfix[count++]=ch;

            else
            {
              while(s.priority((char)s.peek())<=s.priority(ch))
              {
                postfix[count++]=s.pop();
              }

              s.push(ch);   
            }
         }


         while(!s.isEmpty())
         {
            postfix[count++]=s.pop();
         }
         postfix[count]=null;
         System.out.print(postfix);




    }
    public String toString()
    {
      StringBuffer sb=new StringBuffer();
      sb.append("[");
      for(int i=top-1;i>-1;--i)
      {
        sb.append(stack[i]+",");


      }
      sb.append("]");  
      return sb.toString();

    }

Why i am having error like this?

java.lang.ArrayIndexOutOfBoundsException: -1

This is probably the error:

while(s.priority((char)s.peek())<=s.priority(ch))

What is the cast in this line supposed to do? What does the peek() method return?
Can it be cast to the primitive type: char?

Edited 4 Years Ago by NormR1

Line 39

public Object peek()
    {
        return stack[top-1];

    }

Line 97 is pointing to while loop

for(int i=0;i < cc.length;i++)
         {

             ch=cc[i];

            if(Character.isDigit(ch)) 
                postfix[count++]=ch;

            else
            {
  Line 97==>    while(s.priority((char)s.peek())<=s.priority(ch))//Line 97..
              {
                postfix[count++]=s.pop();
              }

              s.push(ch);   
            }
         }

@NormR1,...The return of the method peek is Object:,I made cast on it because the parameter of the method priority is char,that's why i casted it to char.

You can not cast an object to a primitive. You will need to get the object, cast it to the class it is and call one of its methods to get the value to compare or to do the compare directly.

@NormR1,I do apologize,I could not get you...

You will need to get the object

and here.

cast it to the class it is and call one of its methods to get the value to compare or to do the compare directly.

Quoted Text Here

peek() returns an Object. What class is it? Can you cast the Object to its type?
When you get its true class, then you can call one of that class's methods to get to its contents so you can compare it.

What is the class of the object returned by peek()? Print it out to see.

@NormR1,Can you please show me how,what exactly you mean.

@NormR1, peek() is in the class of InfixToPostfix? am i rigth?

For debugging add this before the while() loop
System.out.println(s.peek()); // show what peek() returns

When you see what type peek() returns then you will know what class to cast it to.

TheClass aVar = (TheClass)s.peek(); // get the object and cast it to the type
char aChar = aVar.getChar(); // call a method to get a char value from the object

@NormR1,how can i print the class peek()?

What is the class of the object returned by peek()? Print it out to see.

Quoted Text Here

@NormR1,I tried to print but it will gives me error i cannot see the class.

     char [] cc = Infix.toString().toCharArray();
         for(int i=0;i < cc.length;i++)
         {

             ch=cc[i];

            if(Character.isDigit(ch)) 
                postfix[count++]=ch;

            else
            {
               System.out.print(s.peek());  
//            while(s.priority(s.peek())<=s.priority(ch))
//            {
//              postfix[count++]=s.pop();
//            }
//            
//            s.push(ch);   
            }
         }


//       while(!s.isEmpty())
//       {
//          postfix[count++]=s.pop();
//       }
//       postfix[count]=null;
//       System.out.print(postfix);
//       







Enter infix: 1+2+3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at InfixToPostfix.peek(InfixToPostfix.java:39)
    at InfixToPostfix.main(InfixToPostfix.java:97)

Process completed.

it will gives me error

Please post the full text of the compiler's error message.

What is the source line (line 39) the error is on?

Edited 4 Years Ago by NormR1

Line 39

public Object peek()
    {
        return stack[top-1];

    }

If top was 0 then there would be this exception:

ArrayIndexOutOfBoundsException: -1

What should peek() do if the stack is empty?

@NormR1, I changed to this

public Object peek()
    {
    //  return stack[top-1];

       Object s =null;

       if(!this.isEmpty())
          s = stack[top];
       else
         System.out.print("Stack is empty");      
      return s;
    }



Enter infix: 1+2+3
Stack is emptyException in thread "main" java.lang.NullPointerException
    at InfixToPostfix.main(InfixToPostfix.java:104)

It the stack is empty, peek() returns a null value now.
All callers of peek() will have to test if the value returned is null. Or test if the stack is empty before calling peek().

@NormR1,is this what you mean?

    InfixToPostfix s =new InfixToPostfix(1);


        Object[] Infix=new Object[1];

        Object[] postfix={0
        };

        int count=0;
        char ch='\u0000';

         System.out.print("Enter infix: ");
         for(int i=0;i<s.stack.length;i++){
                Infix[i]=new java.util.Scanner(System.in).next();
         }

         char [] cc = Infix.toString().toCharArray();
         for(int i=0;i < cc.length;i++)
         {

             ch=cc[i];

            if(Character.isDigit(ch)) 
                postfix[count++]=ch;

            else
            {
              if(!s.isEmpty())
              {
                  Character x = (char)s.peek();
                  while(s.priority(x.charValue())<=s.priority(ch))
                  {
                    postfix[count++]=s.pop();
                  }

                  s.push(ch);   
              }     
            }
         }


         while(!s.isEmpty())
         {
            postfix[count++]=s.pop();
         }
         postfix[count]=null;
         System.out.print(postfix);

@NormR1..i think i got it but why is the ouput look like this

Enter infix: 1+2+3
[Ljava.lang.Object;@1786e64
Process completed.

Here is my code....

public class InfixToPostfix
{
     Object[] stack;
     int top;

    public InfixToPostfix(){
    }

    public InfixToPostfix(int size)
    {
        stack=new Object[size];
        top=0;
    }

    public boolean isEmpty()
    {
        return top==0;
    }

    public boolean isFull()
    {
        return top==stack.length;
    }

    public boolean push(Object item)
    {
        boolean ok=!isFull();
        if(ok)
        {
            stack[top++]=item;
            ok= true;
        }

        return ok;
    }

    public Object peek()
    {
    //  return stack[top-1];

       Object s =null;

       if(!this.isEmpty())
          s = stack[top];
       else
         System.out.print("Stack is empty");      
      return s;
    }

    public Object pop()
    {
       Object item=peek();
       if(item!=null)
       {
         stack[top-1]=null;
         top--;

       }
        return item;
    }

    public int priority(char c)
    {
        int p=999;

        switch(c)
        {
            case '*': case  '/': p=0; break;
            case '+': case  '-': p=1; break;
        }
        return p;
    }


    public static void main(String...args)
    {
        InfixToPostfix s =new InfixToPostfix(1);


        Object[] Infix=new Object[1];

        Object[] postfix=new Object[10];

        int count=0;
        char ch='\u0000';

         System.out.print("Enter infix: ");
         for(int i=0;i<s.stack.length;i++){
                Infix[i]=new java.util.Scanner(System.in).next();
         }

         char [] cc = Infix.toString().toCharArray();
         for(int i=0;i < cc.length;i++)
         {

             ch=cc[i];

            if(Character.isDigit(ch)) 
                postfix[count++]=ch;

            else
            {
              if(!s.isEmpty())
              {
                  Character x = (char)s.peek();
                  while(s.priority(x.charValue())<=s.priority(ch))
                  {
                    postfix[count++]=s.pop();
                  }

                  s.push(ch);   
              }     
            }
         }


         while(!s.isEmpty())
         {
            postfix[count++]=s.pop();
         }
         postfix[count]=null;
         System.out.print(postfix);




    }
    public String toString()
    {
      StringBuffer sb=new StringBuffer();
      sb.append("[");
      for(int i=top-1;i>-1;--i)
      {
        sb.append(stack[i]+",");


      }
      sb.append("]");  
      return sb.toString();

    }

Can you explain what the code you posted is supposed to do? I don't see any comments in the code that would help anyone understand what it is supposed to do.

Does it compile and execute and give you the correct results?

@NormR1...Th result is this

Enter infix: 1+2+3
[Ljava.lang.Object;@197a37c
Process completed.

This question has already been answered. Start a new discussion instead.