Click to See Complete Forum and Search --> : Searching a String Array
fortune2k
April 19th, 2008, 08:12 AM
Hi,
At the moment I have a string array setup with:
ID:2 Avaliablitiy: Available Name:Daveship
What I would like to do is search this array for "Available" and replace it with Unavailable. Does anyone know if this can be done and if so how to do it?
Thanks.
dlorde
April 19th, 2008, 08:40 AM
Er, you can loop through the array item by item, and for each item String you can use the String.replaceAll(..) method to replace all instances of 'Available' with 'Unavailable' in the String...
Is that what you wanted?
The biggest difference between time and space is that you can't reuse time...
M. Furst
fortune2k
April 19th, 2008, 08:48 AM
Not really. What ive got to do is have a boat hire company with rowingboats and pedal boats. Ive manged to make constuctors that will create the boats such as:
public RowingBoat(String a, int s, String Name)
Now ignoring pedal boats, my rowingboat array will look like this:
ID:1 Avaliablitiy: Available Name:Tedship
ID:2 Avaliablitiy: Available Name:Daveship
ID:3 Avaliablitiy: Available Name:Peteship
and what I want to do is be able to change a rowingboat from Available to Unavailable when it is hired out (this will be changed with user input from a menu)
So bassically what its got to do is search the string array for "Available" and replace it with Unavailable. Can this be done?
dlorde
April 19th, 2008, 09:15 AM
So bassically what its got to do is search the string array for "Available" and replace it with Unavailable. Can this be done?? That's just what I described in my previous email... can you explain a bit more how what you want to do is different from what I suggested?
But I don't understand why you are storing that information in the array concatenated as a single String. If you have a RowingBoat class, why not use that in the array? When you want to chage a boat from available to unavailable, you only need to call boat.setAvailable(false) (assuming you have such a method), which sets the value of the boolean 'available' flag in Boat. Then if you want to know whether a boat is available, you can call boat.isAvailable(), which returns the value of the boolean 'available' flag in Boat.
It is better to have an approximate answer to the right question than an exact answer to the wrong one...
J. Tukey
fortune2k
April 19th, 2008, 10:04 AM
I think ive done it, thankyou for your help
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.