Results 1 to 4 of 4
- 04-24-2013, 01:15 AM #1
Member
- Join Date
- Apr 2013
- Posts
- 2
- Rep Power
- 0
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
- 04-24-2013, 01:56 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 04-24-2013, 03:01 AM #3
Member
- Join Date
- Apr 2013
- Posts
- 2
- Rep Power
- 0
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.
- 04-24-2013, 03:45 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
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
-
Integer array to String array
By lakshmibvaraprasad in forum New To JavaReplies: 1Last Post: 07-20-2011, 09:39 PM -
How to display array integer in JOptionPane message dialog ?
By Reero8532 in forum New To JavaReplies: 13Last Post: 03-20-2010, 02:03 AM -
change string to array of integer
By prof.deedee in forum New To JavaReplies: 4Last Post: 11-09-2009, 03:47 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 07:40 AM -
Stack not popping
By bugger in forum New To JavaReplies: 2Last Post: 01-28-2008, 05:59 PM
Bookmarks