Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2004
    Location
    UK
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help! Problems with a Java stack

    Hi there,
    I am new to the world of java and programming itself. In a lecture my lecturer went through pushing and popping on a stack and we wrote out a class for it. When I got back to my house and tried to write up the code I just keep getting the same errors and I can't figure out whats wrong with it. I have copied it exactly the same as his.
    This is the error i got:

    C:\Documents and Settings\Edd\Desktop\Java\IntStack.java:3: <identifier> expected
    Static int[] IStack;
    ^
    C:\Documents and Settings\Edd\Desktop\Java\IntStack.java:4: <identifier> expected
    Static int[] numberOfElements;
    ^
    C:\Documents and Settings\Edd\Desktop\Java\IntStack.java:6: <identifier> expected
    Static void setSize (int pSize)
    ^
    C:\Documents and Settings\Edd\Desktop\Java\IntStack.java:48: ';' expected
    }
    ^
    4 errors

    Tool completed with exit code 1

    And this is the code:

    public class IntStack
    {
    Static int[] IStack;
    Static int[] numberOfElements;

    Static void setSize (int pSize)
    {
    IStack = new int [pSize];
    }
    Static int getSize()
    {
    return IStack.length;
    }

    Static void push(int pItem) throws Exception
    {
    //is it full?
    if (isFull())
    throw new exception ("Stack is full");
    else
    IStack[numberOfElements++] = pItem;
    }

    Static int pop() throws Exception
    {
    //is stack empty?
    if(isEmpty())
    throw new Exception("Stack is empty");
    else
    return IStack[--numberOfElements];
    }

    Static boolean isEmpty()
    {
    if(numberOfElements < 1)
    return true;
    else
    return false;
    }

    Static boolean isFull()
    {
    if(IStack.length == numberOfElements)
    return true;
    else
    return false;
    }
    }


    If anyone could help me that would be really cool. I am sure its just something simple that I have missed out (it usually is!). Thanks for your time.
    Last edited by SuperSharp; 12-01-2004 at 11:45 AM.

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Simpler than you think: it's static and not Static.
    But to tell you the truth, I would just get rid of all the "static"s in your code. With the static declarations you will only have one stack per program. Without them you can create several independent instances of IntStack and each of them will have separate data.

    shmoove

  • #3
    New to the CF scene
    Join Date
    Nov 2004
    Location
    UK
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah thats amazing, thanx man! Muchly appriciated!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •