learn java
The moose likes Java in General and the fly likes Vector problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector problem" Watch "Vector problem" New topic

Vector problem

Luke Chadwick

Joined: Mar 05, 2004
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

Joined: May 29, 2003
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)

Joined: Jun 27, 2002
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,

Java Regular Expressions
Luke Chadwick

Joined: Mar 05, 2004
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");
Removing the substring doesnt help. Please help me.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Luke
It says replace(char, char) in java.lang.String CAN'T BE aplied to replace(String, String) right? That means, in String class there is no method replace() having two Strings as argument. It needs two characters. So try,
x.substring(1,3).replace('t','k'); and that should work.
also, don't forget that x is a string hence non-mutable so you have to do,
x = x.substring(1,3).replace('t','k'); if u want to update value of variable x . It will not automatically assign the replace operation's outcome to variable x.
I agree. Here's the link: http://aspose.com/file-tools
subject: Vector problem