Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

# Vector problem

Greenhorn
Posts: 8
I have a vector which contains the following data (just an example of what the vector contains below:-
[J46, J53B0907, J54B0907/1, J78, J6JT4567]
I want to be able to replace the J characters (the first character after each comma) with K characters. It wouldnt be possible to do a global replace on the letter J as J may appear after the first character (as shown near the end).
I have no idea how to do this, would anybody like to help a student out?

Nathaniel Stoddard
Ranch Hand
Posts: 1258
Well, student ... all you need to do is iterate through the elements in your Vector, checking the first character of each String. If it is a "J" replace it with whatever you want, and set the Vector element accordingly.
If you're looking for some actual code here, look up the API docs and work through it. You'll much faster hacking through it yourself.

Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
As a teacher, I like this problem, because it tests a couple of concepts. One, you have to be aware of the immutability of Strings. Two, you have to understand how to add and remove items from a Vector: simply add to the vector won't do the trick. Three, you have to demonstrate some mastery String manipulation. This should be a good learning experience.
I would start small. First, modify the first J in a String: and make sure you don't modify subsequent 'J's. Next, modify the first J in an Array of Strings. Finally, adjust the vector so it holds the modified data.
Best regards,
M

Greenhorn
Posts: 8
Im totally not getting the documentation (Sun's just confuses me) and books that I've looked. I've been trying to do the following:-
x.substring(1,3).replace("t", "k");
where x is a String = "Standing"
But I keep getting the following error
replace(char,char) in java.lang.String cannot be applied to (java.lang.String,java.lang.String)
x.substring(1,3).replace("t", "k");
^