We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,415 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Error in stack

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();

    }


}
4
Contributors
94
Replies
1 Week
Discussion Span
9 Months Ago
Last Updated
95
Views
Question
Answered
ezekel
Junior Poster in Training
52 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

ezekel
Junior Poster in Training
52 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

@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)
ezekel
Junior Poster in Training
52 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
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();

    }
ezekel
Junior Poster in Training
52 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Why i am having error like this?

java.lang.ArrayIndexOutOfBoundsException: -1

ezekel
Junior Poster in Training
52 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Line 39

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

    }
ezekel
Junior Poster in Training
52 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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);   
            }
         }
ezekel
Junior Poster in Training
52 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

@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.

ezekel
Junior Poster in Training
52 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

@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

ezekel
Junior Poster in Training
52 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

ezekel
Junior Poster in Training
52 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

ezekel
Junior Poster in Training
52 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

@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

ezekel
Junior Poster in Training
52 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1281 seconds using 2.81MB