• Post Reply Bookmark Topic Watch Topic
  • New Topic

iterate display of integer array for a stack in GUI  RSS feed

 
Nicola Alset
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an integer array called "stack[]" that I am using as a stack. This is being displayed in a GUI and everything seems to be working properly except that I cannot
figure out how to display my iterated array as I push and pop in the GUI. The array should be displayed when I push in the GUI such as;

24 <---- first push

24, 36 <---- adding an integer on second push

24, 36, 18 <---- you get the picture... up to a full stack of 10

and then...

24, 36 <---- first pop

24 <---- next pop...

empty <---- empty stack after last pop

How do I iterate an integer into the GUI on each fired event so that the above result is displayed? You can see my latest attempt below. I am vapor locked for some reason
and can't seem to figure out how to write the loops that would allow the display to increase/decrease the array list the way I need. Currently I can only get one integer
of the array to display at a time. Any help would be greatly appreciated.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • There's no need that I can see for separate top and sCount variables. They seem to represent the same thing.
  • You push method is truly bizarre. Why are you placing N different random numbers on top of the stack (throwing out N-1 of them immediately), where N is the lenght of the array, rather than just putting one number there. (You're not getting any more randomness by calling Random.next() a bunch of times, if that's what you're thinking.)
  • You're also totally ignoring the parameter you're passing to push().
  • Your second for loop in push() says, "set the text of field1 to the value of the item on the top of the stack, oh, and make sure to set it to that same value M times (where M appears to be the number of items in your stack).
  •  
    Nicola Alset
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yea the sCount should have been removed. It was a vestige of my trying to figure something out. I see what your saying about the Random.next(). I had it in my head that I need that in a loop for some reason. The parameter being passed to push if for an interface that is only set up as a shell. As for your last note, that is the problem I have been struggling with. I am not sure how to display all of the integers that are currently in the array delimited by commas as push/pop interact with the array. Here's an updated version of push()

     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nicola Alset wrote:YAs for your last note, that is the problem I have been struggling with. I am not sure how to display all of the integers that are currently in the array delimited by commas as push/pop interact with the array


    Okay, but you know how for loops work, right? And you know what field1.setText() does, right?

    So therefore, you must know, without seeing it or without me telling you, that this:



    will just set the text of that field to the stop of the stack N times, where N is whatever value is on top of the stack.

    So if the top of your stack is 3, then that loop does


    and if the top of your stack is 7, it does


    Programming by guessing is not very effective.

    You need to step away from the computer and think about how you would do this "manually", and write those steps down very clearly and precisely.

    So you have a physical stack of cards, and each one has a number written on it, and you want to write those numbers down on a line on sheet of paper, with a comma between each pair of numbers.

    You want to pretty much forget about Java for this exercise, but one thing you'll probably want to keep in mind is that setText() just sets the text to whatever you give it, replacing what was there before. Unless there's an "addText()" or somesuch method, you'll have to have the full string prepared ahead of time.
     
    Nicola Alset
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeff Verdegan wrote:
    Okay, but you know how for loops work, right? And you know what field1.setText() does, right?
    So therefore, you must know, without seeing it or without me telling you, that this:


    will just set the text of that field to the stop of the stack N times, where N is whatever value is on top of the stack.
    Programming by guessing is not very effective.
    You need to step away from the computer and think about how you would do this "manually", and write those steps down very clearly and precisely.
    So you have a physical stack of cards, and each one has a number written on it, and you want to write those numbers down on a line on sheet of paper, with a comma between each pair of numbers.
    You want to pretty much forget about Java for this exercise, but one thing you'll probably want to keep in mind is that setText() just sets the text to whatever you give it, replacing what was there before. Unless there's an "addText()" or somesuch method, you'll have to have the full string prepared ahead of time.



    Thanks for the ideas. I am very new to java and I appreciate the help. I managed to figure out the initial problem and get the result I was looking for. This then created another problem for me in the pop() method that I am working on now. Now that I have my comma delimited string displayed in the GUI via the push() method, I need to be able to remove items from that GUI display one at a time. The pop() method that I have is able to do basically what I need with respect to the array, but I can't figure out how to remove items from the GUI. Currently as I pop items from the array the display shows the array with the ", 0" replacing each array element as they are popped. What I can't figure out is how to pop those elements without sticking a placeholder like 0 in place of the original element or trimming the ", 0" placeholder afterward. I am also not sure how to get rid of the [] that enclose the array when I use Array.toString(). I looked at various ideas related to trim(), remove() and numerous other ideas but I can't seem to find one that fits the problem.

    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]

    Here is the code I have at the moment;




     
    Nicola Alset
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alright, I managed to get to a point where I am able to remove numbers from the text field of the GUI as I pop integers from the array. I am now left with the vestigial commas that are left behind as elements are removed from the array. How would I be able to make those commas disappear as integers are removed via the pop() method. I looked into a few ways to loop a regex for the indexes above top but I can't seem to find a way to do it that works. Please keep the laughter to a minimum when you look at my code. I know it is far from elegant.

     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!