remove element from stack

Ads
 

remove element from stack

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);
    }

} }

View Answers

April 2, 2012 at 5:39 PM

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);
      }
    }
  }
}

Ads









Related Tutorials/Questions & Answers:
remove element from stack
remove element from stack  Hi there I'm looking around on the internet how to remove names from stack by asking user how many names they want... error message and if not then remove the names from stack until the stack is empty
How to remove specific element from Hashset.
How to remove specific element from Hashset. In this  exmple, you will see how to remove specific element from hashset. The HashSet class provides a method called remove. It removes a elements from set. Code: 
Advertisements
How to remove all element from Hashset.
How to remove all element from Hashset. HashSet is set type Collection...; it .In this example, you will see the use of clear method of HashSet. It is used to remove all element of hashset. Code:  HashSetRemoveElement.java
write a program to remove the element from hashset using keyboard.
write a program to remove the element from hashset using keyboard.  write a program to remove the element from hashset using keyboard
remove parent element JavaScript
remove parent element JavaScript  How to remove parent element in JavaScript
JavaScript Remove Element
JavaScript Remove Element...; In this section, you will learn how to remove HTML element using JavaScript. JavaScript provides several methods which allow the user to remove particular HTML element
How To Remove Array Element In Java
will demonstrate you about how to delete an element from array. To remove the array...How To Remove Array Element In Java In this section we will discuss about how to remove a given array element. In this section we will discuss about removing
JavaScript Array Remove Element
JavaScript Array Remove Element In this example we will describe JavaScript Array Remove Element. First of all we have created an array list and store... Function. In this example we have used remove() method to remove element
remove comma from string php
remove comma from string php  How to remove the last comma from the string in PHP
remove comma from string php
remove comma from string php  How to remove the last comma from the string in PHP?   remove last comma from string names = names.replaceAll(",$", ""); //Or a simple substring: if (names.endsWith(",")) { names
remove from superview iphone sdk
remove from superview iphone sdk  How to remove the old view from super-view and add the new view safely? My main concern is memory leak.. so, please suggest how can i do it without facing a memory leak. Thanks
remove punctuation from string in java
remove punctuation from string in java  Hi, I want to remove punctuation in text from Java program. How to remove punctuation from string in java? Thanks   Hi, You can use the regular expression "\p{Punct
remove all objects from NSMutablearray
remove all objects from NSMutablearray  Hi, How to remove all objects from nsmutablearray? Thanks   HI, Here is the code for creating NSMutablearray, adding objects and then removing all the objects: NSMutableArray
Java Stack Example
the element to stack, pop to remove the element from stack, peek to look... the item in the stack and how far it is from top of stack Example : A Java code.... System.out.println(st.pop());//Remove the item on top of the stack
How to Remove audio from video file with FFmpeg?
How to Remove audio from video file with FFmpeg?  I have a video test.mp4 and I want to remove the audion from the video. So, that there is no audion in video. How to remove audio from video file with FFmpeg? thanks   
How to Remove Repeated Characters from the String?
How to Remove Repeated Characters from the String?  Hi, I trying to develop an application which will remove the repeated characters from... the example of how to remove repeated characters from the string. Please visit
Need to Remove Duplicate Records from Excel Sheet
Need to Remove Duplicate Records from Excel Sheet  Need to Remove Duplicate Records from Excel Sheet. I have one excel sheet having two fields... empnum rating (without using sql query have to remove records from excel using java
Neo4j - How to to remove all data from neo4j?
Neo4j - How to to remove all data from neo4j?  Hi, I am learning... to delete all nodes, links and properties from a Neo4j database? Thanks  ...]-() DELETE n,r This query will delete all the data (nodes, links, properties) from
Remove duplicate characters from the string
Remove duplicate characters from the string In this tutorial, you will learn how to remove duplicate characters from the string. Java has provide several.... Here we are going to remove duplicate characters from the string
how to remove the column from a java web page
how to remove the column from a java web page  i have a web page with account#, qtr, year if i want to remove the year column which is a drop down list from my jsp what should i do and what is the process please give a brief view
How to remove paragraph from ms word
How to remove paragraph from ms word  Hi! I am using MS Word to edit my text but somehow i get paragraph sign on every line of my text. So, can any one please tell me how to remove paragraph from ms word?   CTRL+SHIFT+8
Java Remove a character from string
Java Remove a character from string In this tutorial, you will learn how to remove a character from the string. There are several methods for examining... to remove a particular character from the string. In the given example, we have
Stack
Stack  How to implement a stack using classes in java?   Here is an example that implements a Stack class based on arrays. public class Stack { private int top; private int[] storage; Stack(int
remove duplicates from list in java using set
remove duplicates from list in java using set  Hi, I have a list... example: Remove duplicates from ArrayList Thanks... this? Tell me program for the removal of duplicates from list in java using set
How to remove virtual keypad from apps java
How to remove virtual keypad from apps java  I have an galaxy smart s6830 touch Chinese Mobile (240x320) (myriad java platform) (esmertec jbed) and I need to remove keypad from apps . I have put the following into the jar file
How to remove virtual keypad from apps java
How to remove virtual keypad from apps java  I have an galaxy smart s6830 touch Chinese Mobile (240x320) (myriad java platform) (esmertec jbed) and I need to remove keypad from apps . I have put the following into the jar file
How to remove virtual keypad from apps java
How to remove virtual keypad from apps java  I have an galaxy smart s6830 touch Chinese Mobile (240x320) (myriad java platform) (esmertec jbed) and I need to remove keypad from apps . I have put the following into the jar file
Java Stack Example
();//remove 'Example' from the stack //check whether the top element... is the operation in which the top element is retrieved without removing it from stack... it from the stack. Generally, this method is used to delete the top element
How to remove glass from the face
How to remove glass from the face By this example you will be able to remove any object from every place but. I have tried to remove a goggle from... Tool (S key) and adjust the setting to remove glass. Remove : Hold Alt key
Find Array element from index using GUI
Find Array element from index using GUI In this tutorial, you will learn how to create an GUI application to find an array element from its index... to display the element at that specific index. A button 'Show Element'
Collection : Stack Example
operation of stack System.out.println("Pop element from stack : " + stack.pop()); System.out.println("Pop element from stack : " + stack.pop...] Pop element from stack : world Pop element from stack : hello Size of stack : 1

Ads