Results 1 to 4 of 4
  1. #1
    skyward is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Display string in GUI after popping an integer from array.

    I have tried everything I can think of to display a string in GUI while removing a number each time I pop an integer from the array the string represents. At the moment each pop() replaces the top integer with "0". I have been unable to find a way to avoid using a place holder such as "0" or find a way to cut them afterward. Is there a way to pop integers from an array and then display the array as a string as it is incrementally popped?

    Here is what would be displayed in GUI when I have hit pop for the last 2 elements;

    [12, 23, 43, 65, 38, 60, 26, 51, 0, 0]

    I need it to be;

    [12, 23, 43, 65, 38, 60, 26, 51] when I pop the last 2 elements.

    Java Code:
       public int pop() {	  
    	  stack[top] = 0;
    	  stackList = Arrays.toString(stack);   
    	  field2.setText(stackList);  
    	  top--;
    	  return top;
      }//End pop method

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,784
    Rep Power
    5

    Default Re: Display string in GUI after popping an integer from array.

    When you say display a string in a GUI does that mean you are displaying it in a graphics context, text area, or to the console? What is preventing you from writing a routine to display the active stack? Finally, why are you replacing the popped value with a zero? It is just a memory location that no longer contains a value of interest. So just ignore it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    skyward is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Re: Display string in GUI after popping an integer from array.

    I am trying to display the array in a text field. Frankly, I was replacing the popped element with a zero because it was a marker for me to see that the element had been removed. I would have preferred simply doing something like stack[top] = ""; but I am unable to do that. I couldn't figure out how to get the string representation of the array with the elements removed as I call the pop() method.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Display string in GUI after popping an integer from array.

    Why are you using an array (with its fixed length) when you are using the thing like a stack?

    ---

    If it's because this is some sort of homework where you are supposed to implement a stack using an array, then consider writing a method whose job it is is to construct the string. The method would examine the array elements from the top to the end of the array using a StringBuilder or similar to build up the string it would return. It really is a bit ugly to abuse zero in this way: what if your stack were to be used in situations where the stack should contain zero?

Similar Threads

  1. Integer array to String array
    By lakshmibvaraprasad in forum New To Java
    Replies: 1
    Last Post: 07-20-2011, 08:39 PM
  2. Replies: 13
    Last Post: 03-20-2010, 01:03 AM
  3. change string to array of integer
    By prof.deedee in forum New To Java
    Replies: 4
    Last Post: 11-09-2009, 02:47 AM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. Stack not popping
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 04:59 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •