...

View Full Version : Help! Problems with a stack



SuperSharp
12-01-2004, 12:10 PM
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. :confused:
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. :cool:

shmoove
12-01-2004, 01:09 PM
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

SuperSharp
12-01-2004, 01:15 PM
Ah thats amazing, thanx man! Muchly appriciated! :cool:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum