aspose file tools*
The moose likes Java in General and the fly likes Help Please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help Please" Watch "Help Please" New topic
Author

Help Please

Alan Green
Greenhorn

Joined: Sep 07, 2003
Posts: 17
Hey I am trying to create two stacks, using a while loop to input ints from the keyboard until a negative int is input onto the stack. THis is what I have so far.
import java.io.*; // for I/O
//////////////////////////////////////////////////////////////
class StackX
{
private int maxSize; // size of stack array
private int[] stackArray;
private int top; // top of stack
//--------------------------------------------------------------
public StackX(int s) // constructor
{
maxSize = s; // set array size
stackArray = new int[maxSize]; // create array
top = -1; // no items yet
}
//--------------------------------------------------------------
public void push(int j) // put item on top of stack
{
stackArray[++top] = j; // increment top, insert item
}

//--------------------------------------------------------------
public int pop() // take item from top of stack
{
return stackArray[top--]; // access item, decrement top
}
//--------------------------------------------------------------
public int peek() // peek at top of stack
{
return stackArray[top];
}
//--------------------------------------------------------------
public boolean isEmpty() // true if stack is empty
{
return (top == -1);
}
//--------------------------------------------------------------
public boolean isFull() // true if stack is full
{
return (top == maxSize-1);
}
//--------------------------------------------------------------
} // end class StackX
////////////////////////////////////////////////////////////////
class program1
{
public static void main(String[] args)throws IOException
{
StackX theStack = new StackX(5); // make new stack
StackX bigStack = new StackX(5);

// (1) Insert input loop here
while( !theStack.isEmpty() ) { // until stack is empty,
// delete top item from stack
int value = theStack.pop();
System.out.print(value); // display it
System.out.print(" ");
} // end while
System.out.println("");
} // end main()
} // end class program1
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

So, and the question is...
My question is: Did you see the 'CODE' button?
give it a try!
And remove comments like this:
import java.io.*; // for I/O
Thanks in advance


http://home.arcor.de/hirnstrom/bewerbung
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help Please