Solved

Looking for help in creating a class to handle a stack

Posted on 2007-11-14
8
170 Views
Last Modified: 2013-11-23
Hi Fellow EE'rs

Could someone please assist me with creating code to handle a stack? What would be the required driver programs needed for this?

Many Thanks
Steve
0
Comment
Question by:Stephen Manderson
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20286511
0
 
LVL 5

Expert Comment

by:deeppra
ID: 20286607
Its already there in the JavaSE so u dont need to recreate it just use that
0
 

Expert Comment

by:huzefaq
ID: 20286627
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 19

Author Comment

by:Stephen Manderson
ID: 20286686
Thanks Objects

Not at my own pc but does the following look ok to setup a stack and add from keyboard input?

public class CChar {

      private char CharVal;


      public CChar() {
            Stack stack = new Stack();
            CharVal = ' ';
      }


      public void setCharValFromKeyboard() {
            try {
                  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                  System.out.print("Character to add to stack: ");
                  CharVal = (char) in.read();
                        stack.push(CharVal);
                  System.out.println();

            }
            catch (Exception e) {
            }

      }
   
}
0
 
LVL 5

Expert Comment

by:deeppra
ID: 20286731
Yea its okay
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20286744
close , try more like this:

public class CChar {

       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      Stack stack = new Stack();

      public CChar() {
      }


      public void setCharValFromKeyboard() {
            try {
                  System.out.print("Character to add to stack: ");
                  char charVal = (char) in.read();
                  stack.push(charVal);
                  System.out.println();

            }
            catch (Exception e) {
            }

      }
   
}
0
 
LVL 19

Author Comment

by:Stephen Manderson
ID: 20286829
Thanks for that

Would it just be a case of the following to remove stack values and to print a stack contents?

public void popStackValue() {
       try {
             System.out.println("The poped value is " + stack.pop());
             System.out.println("The new stack size is" + stack.size());
             }
}

public void printStack() {
      try {
            System.out.println("The contents of  the Stack are" + stack);
            }
}

Would that be the correct way to print out the stack contents?

Many Thanks
Steve
0
 
LVL 92

Expert Comment

by:objects
ID: 20286880
yes, that would be a good start
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 88
Oracle SQL syntax check  without executing 6 74
SHA2 certs for IIS AND Java? 2 113
How do I remove an object from a 3 40
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question