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:
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: