Skip to main content
gamedev.net
🔒 Locked

[java] string manipulation

Started by Elehisie Nov 21, 2005 at 12:40 PM 3 replies 750+ views
Original Post
Elehisie
Elehisie
hello guys! Im writing some code to handle a gazillion buttons from my swing gui. so... i set the "ActionCommand" property in each of the buttons in a way that i can see right away which button was pressed. so far so good. I get strings of the form: "qxby" where x can go from 1 to as many "questions" there are and y can be only 1, 2 or 3. q refers to "question". every "question" is composed of 3 radio buttons (yes, no, maybe). and b refers to the radiobuttons, as answers for the questions... so if you click on "maybe" radio button that goes with the "question number 28" the string will be: "q28b3" i want to store all answers in a list, making the index in the list match the number of the question. ok? so answer[5] is the answer for the 6th question, and i can get its value by doing: answer.getElementAt(5). so far so good... its just im having trouble indexing right. why? cuz there must be as many positions in the list as questions, but i have a lot more radiobuttons, for every question has 3 radiobuttons. so every time a radiobutton is clicked, i need to store an answer. but the index of the answer list aint the same as the radiobutton's index. are you following? i also have a "commandslist" which i use to index the radiobuttons. the commands list goes as: 0 q1b1 1 q1b2 2 q1b3 3 q2b1 4 q2b2 . . . and the answer list would go like: 0 Y --> answer for first question, meaning Yes, or the radiobutton b1 1 Y 3 N 4 M . . . so i cant use the same var to index both lists, as one has far more elements than the other. i guess you get the idea. so, to make sure i get the proper index, and store the answer in the right position in the answer list, I want to read the number of the question from the ActionCommand string. so, inside the ActionPerformed method, i can get my "event.getActionCommand()" and it will read something like "q29b2" so... whats left to do: strip the "garbage" from that string, making it "29" so i can properly convert it to a int. like: removed --> q / 29 / b2<-- removed how would my event.getActionCommand().subString(xxxxx) statement go? i figure i need 2 of them? i suck on string manipulation and if there are better ways to do it.. be my guest :D
choffstein
choffstein
event.getActionCommand().substring(1, event.getActionCommand().indexof('b'));

substring is [,). (inclusive with the first, up to, but not including the last index.)
Elehisie
Elehisie
ah ^^
i see it now thx thx! ^_^
--
i never find this "right to the point topics" when im searching java.sun.com ...
Optus
Optus
Is there any reason you're stuck having 'q' and 'b' as seperators? This doesn't make any sense to me. Why don't you make the ActionCommand for question 29 answer maybe to "29 3" instead of "q29b3". Then in your listener just do:
String[] values = event.getActionCommand.split(' ');int questionNum = Integer.parse(values[0]);int answerNum = Integer.parse(values[1]);


This is the easiest way I can see.
Elehisie
Elehisie
hmm that does look more clever...

i started with q and b as separators for debugging reasons... then they just stayed there...

OBS: "hmm so thats how split works...."

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.