
Hi there
I'm looking around on the internet how to remove names from stack by asking user how many names they want to remove and if the number they enter is bigger than the stack number then show error message and if not then remove the names from stack until the stack is empty the exit.
asking for your idea and here is my code
can you please show me how to do it..look forward for your reply
import java.util.*;
public class Stack extends ArrayList<String> {
public Object peek()
{
return get(size() - 1);
}
public Object pop(String s)
{
return remove(size() - 1);
}
public String push(Object obj)
{
add(obj.toString());
return obj.toString();
}
public int search(Object obj)
{
return lastIndexOf(obj);
}
public static void main(String[] args)
{
int d = 5;
String name;
Stack stringSS = new Stack();
Scanner scan = new Scanner(System.in);
for(int i = 1; i <= 5; ++i)
{
System.out.println("Stack contains: " + stringSS.size() + " elements");
System.out.print("Enter string #" + i + ": ");
stringSS.push(scan.nextLine());
}
System.out.println("\nDisplay Stack: " + "\n" + stringSS);
//System.out.println("\nEnter number of employees to be dismissed: ");
Scanner scan2 = new Scanner(System.in);
for(int i = 1; i <= 5; ++i)
{
System.out.println("Stack contains: " + stringSS.size() + " elements");
System.out.print("Enter string #" + i + ": ");
stringSS.pop(scan2.nextLine());
System.out.println("\nDisplay Stack: " + "\n" + stringSS);
}
} }

import java.util.*;
public class Stack extends ArrayList<String> {
public Object peek()
{
return get(size() - 1);
}
public Object pop()
{
return remove(size() - 1);
}
public String push(Object obj)
{
add(obj.toString());
return obj.toString();
}
public int search(Object obj)
{
return lastIndexOf(obj);
}
public static void main(String[] args){
int d = 5;
String name;
Stack stringSS = new Stack();
Scanner scan = new Scanner(System.in);
for(int i = 1; i <= 5; ++i){
System.out.println("Stack contains: " + stringSS.size() + " elements");
System.out.print("Enter string #" + i + ": ");
stringSS.push(scan.nextLine());
}
System.out.println("\nDisplay Stack: " + "\n" + stringSS);
System.out.println("\nEnter number of employees to be dismissed: ");
int num=scan.nextInt();
if(num>d) {
System.out.println("Number you have entered is bigger than the stack size!");
}
else{
for(int i = 1; i <= num; ++i){
System.out.println("Stack contains: " + stringSS.size() + " elements");
stringSS.pop();
System.out.println("\nDisplay Stack: " + "\n" + stringSS);
}
}
}
}