The Artima Developer Community
Sponsored Link

Java Answers Forum
Integer Stacks

1 reply on 1 page. Most recent reply: Sep 20, 2003 6:34 PM by Senthoorkumaran Punniamoorthy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Isa

Posts: 6
Nickname: isa
Registered: Sep, 2003

Integer Stacks Posted: Sep 20, 2003 4:18 PM
Reply to this message Reply
Advertisement
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
}


//--------------------------------------------------------------
publi c 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


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Integer Stacks Posted: Sep 20, 2003 6:34 PM
Reply to this message Reply
Go through the link below for example on reading from standard input.

http://www.javaalmanac.com/egs/java.io/ReadFromStdIn.html

You might have to use Integer.parseInt(String) to get the integer value of the string read.

By the way why are you writing yout own stack when there is a Stack class in Java.

http://java.sun.com/products/jdk/1.2/docs/api/java/util/Stack.html

Flat View: This topic has 1 reply on 1 page
Topic: JSP & Struts - displaying a collection Previous Topic   Next Topic Topic: Is there a jar for    com.bruceeckel.*     ?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use