user3818569
user3818569

Reputation: 1

why isnt "stack[top--] ;" a valid statement

I have pointed to the line with an arrow mark. The Java compiler says this expression isn't valid. Why?

I'm a beginner at java, thanks in advance :)

class stack {

    stack(int size) {
        private int top = -1;
        private int stck[] = new int[size];
    }

    void push(int item) {

        if (top == stck.length - 1)

            System.out.println("StackFull");
        else
            stck[++top] = item;
    }

    int pop ()
        {   
            if (top == -1) System.out.println("StackEmpty");
            else
                 stck[top--] ; <<========= Java Compiler says this expression isnt valid why ?
        }

    void Display() {
        for (int x : stck) {
            System.out.println(x);
            return;

        }
    }
}

Upvotes: 0

Views: 106

Answers (5)

nogard
nogard

Reputation: 9716

Because you haven't declared top member variable properly, you did it in constructor, but it should be in the class scope:

class Stack {
    private int top = -1 ;
    private int stck[];

    // constructor
    public Stack(int size) {
        stck = new int[size];
    }
}

If you declare variable in the class constructor it will be just local variable and will not be visible to the rest of the methods

Upvotes: 1

Ian Roberts
Ian Roberts

Reputation: 122374

Aside from the fact that stck and top need to be fields rather than local variables (as explained in other answers), another problem is that the pop method is declared to return int, but it's possible (in fact certain) for the method body to "complete normally" without an explicit return or throw. This isn't allowed for non-void methods. You need to explicitly return the value you pop off the stack, and the failure case should probably throw an exception instead of just printing a message.

int pop() {
  if(top == -1) {
    throw new EmptyStackException();
  }
  else {
    return stck[top--];
  }
}

Likewise, it would be more idiomatic java to have push throw an exception when the stack is full, rather than failing silently.

Upvotes: 0

davmac
davmac

Reputation: 20631

stck[top--] ; simply accesses an array element - it doesn't do anything with it. This isn't allowed in Java, because it usually means you forgot to do something with the value.

In this case, I'm guessing you wanted to:

return stck[top--];

However, you also need to handle the "stack empty" case properly. You still need to exit the method; I'd suggest you should do so by throwing an exception in this case.

Upvotes: 0

Sagar D
Sagar D

Reputation: 2618

Problem 1:

class Stack {
    private int top = -1 ;
    private int stck[] = new int[SIZE] ;

    public Stack() {
    }
}

Problem 2:

int poppedItem = stck[top--];

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

This is not a issue with stck[top--] ;. You need to introduce a variable here else it is not a valid Java statement

Like

int a=stck[top--];

Not only here But also you will have error in following too

int[] arr=new int[5];
arr[2]; // not a valid statement

You need to assign it to variable

int ex=arr[2];

Upvotes: 0

Related Questions