Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Storing string variable value in each button action

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm coding some kind of D&D generator as practice and I want the user to be able to input the enemies that will be generated. I have a class called "Villanos" (which means villains in spanish) with only one property (String name). Then, I have  GUI with one TextField and 2 buttons (Add & Generate). The idea is:

1. You enter a name and then click add (villain1.name is stored)
2. Entered text is set to blank again
3. You enter a name and then click add (villain2.name is stored
... and so on until 5 villains.

When you click "Generate" I want to be able to get the name properties of my objects.

Problem: Every click of add doesn't "add up" just keep storing in the same variable. Here is my code and thank you in advance



I tried to do something like



but no luck, any suggestions? Thank you for your time
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually when you are naming variables like "villano1", "villano2", and "villano3" it means you need an array or List.
or
With the array you would need an int MAX_VILLANOS = 3 and with the List, you could test for villanos.size() == MAX_VILLANOS; If the array is at its maximum index or the List is at its maximum size, you would disable the Generate button.
 
Carlos Roma
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, now I see, I tried at first with the ArrayList class but at some point decided it wasn't needed  for this. Maybe I have a misconception but if I have my objects (each with their property 'name') and I want to store and call just the property, I can't do that without the use of the ArrayList class? I need to store the object inside a list?

Thanks a lot!
 
Knute Snortum
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The List interface has many implementations, but in your case an ArrayList is probably the best if you don't know how many objects will be in the collection and you want to be able to get them by index.  You add objects to a List like this:

Each object is stored in the next index, starting at zero. Now if you want to get the villain "Beta", you would write and if you wanted to get the name of villain "Beta" you would write
By the way, it would be better to call your class Villano (if that's the singular of Villanos) since the class describes one villain.
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic