Results 1 to 2 of 2
  1. #1
    kmd
    kmd is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default Comparing String to Int variable

    Hi all! Let's begin with my problem:

    I have String table with few elements (with their individual names). Let say it looks like:

    Java Code:
    String[] elementsTable= {"elementA", "elementB", "elementC"};
    For each element I created Int variables and called them:

    Java Code:
    int elementACounter = 0; 
    int elementBCounter = 0; 
    int elementCCounter = 0;
    In next part of code I implemented a loop, using 'while' method, which takes one element from elementsTable and shows it on screen/console. I want to add method which will count those elements. For instance, if I will take elementB three times, the elementBCounter will have value 3. Of course I can do it manually creating 'if...else' methods for each elementXCounter and element from my table. But it will be impractical for let say about 20 elements. I want to make it more automatic.

    As you can see on example, the elements names in table and names of Int variables differ only in Counter word which is added to Int variables names. My idea is, after drawing lots from table i can add to it's name 'Counter' word - in that way i will have a name of my Int variable. The problem is, that it will be still a String, not Int. I tried to do sth like this:

    Java Code:
    int elementsRange= elementsTable.length - 1; 
    int elementNumber= (int)Math.round(Math.random()*elementsRange); 
    String elementNumberWithCounter = elementsTable[elementNumber]+"Counter"; 
    int elementNumberCounter = Integer.parseInt(elementNumberWithCounter ) + 1;
    What i tried to do was:
    1) Get a random element from Table.
    2) Add 'Counter' to its name. So if i have taken elementA i created elementACounter.
    3) I tried to change it to Int.
    Of course, it doesn't work.

    My question is:
    How to connect/link a String name (a name of some element from Table) with the name of Int variable? Couse I want to use this Int in iterations.
    Or giving a programming problem: If you have taken elementA from Table, increase elementACounter by 1 (without creating a lot os loops and conditional functions).

    Any ideas? :)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: Comparing String to Int variable

    Sounds like a job for a Map.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 8
    Last Post: 02-22-2014, 08:06 AM
  2. Replies: 0
    Last Post: 04-17-2011, 04:57 PM
  3. Replies: 1
    Last Post: 10-21-2010, 07:06 PM
  4. Comparing JComboBox with string?
    By vahshir in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 07:39 AM
  5. Comparing contents of a file with String[]
    By eponcedeleon in forum Advanced Java
    Replies: 2
    Last Post: 02-25-2010, 09:10 PM

Posting Permissions

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