Results 1 to 2 of 2
Thread: Comparing String to Int variable
- 11-17-2011, 07:18 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 1
- Rep Power
- 0
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:
For each element I created Int variables and called them:Java Code:String[] elementsTable= {"elementA", "elementB", "elementC"};
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.Java Code:int elementACounter = 0; int elementBCounter = 0; int elementCCounter = 0;
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:
What i tried to do was: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;
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? :)
- 11-17-2011, 07:33 PM #2
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
-
How to insert node to tree while comparing string instead of int
By Javanoobs in forum New To JavaReplies: 0Last Post: 04-17-2011, 04:57 PM -
Comparing a character with characters within a string
By Civie in forum New To JavaReplies: 1Last Post: 10-21-2010, 07:06 PM -
Comparing JComboBox with string?
By vahshir in forum New To JavaReplies: 2Last Post: 10-10-2010, 07:39 AM -
Comparing contents of a file with String[]
By eponcedeleon in forum Advanced JavaReplies: 2Last Post: 02-25-2010, 09:10 PM -
Comparing string using == or != (how to compare string in if else)
By fiqueudrue in forum New To JavaReplies: 6Last Post: 02-10-2009, 06:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks