12-26-2009, 05:27 PM
(This post was last modified: 01-01-2010, 02:35 AM by Testgamma1.)
Code:
import javax.swing.JOptionPane;
public class Test2
{
public static void main (String [] args)
{
String inp;
int cho, input, index;
int [] number = new int [10];
input=index=0;
do
{
inp=JOptionPane.showInputDialog(" Choose number: \n1. Push \n2. Pop\n 3. Display\n 4. Exit ");
cho =Integer.parseInt(inp);
if (cho == 1)
{
if (input<10)
{
inp=JOptionPane.showInputDialog("Index "+ (input)+(" of 9:"),"");
number[input]=Integer.parseInt(inp);
input++;
JOptionPane.showMessageDialog(null," Updated!","Update Message",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null," Full\n ","Overflow",JOptionPane.ERROR_MESSAGE);
}
}
else if (cho==2)
{
if (number [0]==0)
{
JOptionPane.showMessageDialog(null," Input at least 1 first before pressing 2","No Stack",JOptionPane.ERROR_MESSAGE);
}
else
{
number[input-1]=0;
input--;
JOptionPane.showMessageDialog(null," Updated!","Update Message",JOptionPane.INFORMATION_MESSAGE);
}
}
else if (cho==3)
{
System.out.print("STACK: ");
for (index=0; index<=9; index++)
{
System.out.print("||"+number[index]+"|| ");
}
System.out.print("\n");
System.out.print("INDEX: ");
for (index=0;index<=9;index++)
{
System.out.print("||"+index+"|| ");
}
System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
else if (cho==4)
{
JOptionPane.showMessageDialog(null," Exit!");
}
else
{
JOptionPane.showMessageDialog(null," Invalid Input!");
}
System.out.print("S T A C K:\n");
for (index=0; index<=9; index++)
{
System.out.print("|| "+number[index]+" \n");
}
System.out.print("\n");
System.out.print("I N D E X:\n");
for (index=0; index<=9; index++)
{
System.out.print("|| "+index+"\n");
}
System.out.print("\n");
}
while(cho!=4);
}
}