Subject: Iterating the characters in a string, in java.
Psychopixi
Fanatic
Posts: 376 Registered: 31/12/1969 Status: Offline
posted on 5/1/2006 at 02:36 PM
If anyone could help me with this, I'd be very happy!
I have to write a program in java for my course work, and part of it
involves reading in strings from a file. That's all fine and dandy.
Once I have the string, I need to get the separate characters. For example,
if my string is "otcl" I need to have "o", "t", "c" and "l".
I know how to iterate through the string to get separate characters, but
the length of the strings vary - from 1 to 6 characters. I need to get the
separate characters, and compare them to the characters from subsequent
strings.
It's possible I'm being incredibly stupid about this, seeing as I've been
working on it for ages, and my brain's beginning to shut down...
but...
If I'm iterating through the string, and I don't know how long it will be,
how do I keep track of the characters?
This is my code so far:
CharacterIterator charIt = new StringCharacterIterator(cLikes);
// Iterate over the characters in the forward direction
for (char c = charIt.first(); c != CharacterIterator.DONE ; c =
charIt.next() )
{
//get the separate characters
}
Am I being really stupid? I had the idea of creating an array list, and
adding c in on each iteration. Is that a bad idea?
Gah, I hate coursework.
____________________ Do not fear death so much, but rather the inadequate life.
gothicmorman
Fanatic
Posts: 233 Registered: 11/7/2002 Status: Offline
posted on 6/1/2006 at 01:38 AM
what do you have to compare it to the other ones for? like what are you
looking for in it?
that seems to be a good solution, its what i would do (though i could be
stupid too lol). If you need them all to be the same length you could make
the loop run six times for each one and just pad it to the right with
spaces so each array would be six long,
premake each array to be six long then do your code then go something
like...
if (< arrayname >.Length<6)
{
for (int i=< arrayname >.Length; i<7; i++)
{
< arrayname >[ i ]=" ";
}
}
like i think that would work - idea wise anyway... keep in mind that i
don't actually KNOW Java or Javascript so actual syntax might be off but
logically i think that should work...
in C# there is a .ToCharArray(); function that just.. puts it
in a char array for you... maybe java has one you don't know about?
::edit:: hmm, didn't realize the code part in there lol.. few spaces fixed
that.. except i think what i wrote there was kind of weird anyway...
[Edited on 1/16/2006 by gothicmorman]
Psychopixi
Fanatic
Posts: 376 Registered: 31/12/1969 Status: Offline
posted on 6/1/2006 at 01:59 AM
Sorted - there is a toCharArray function in java... I just didn't know it
existed. Thanks a million for pointing it out!!
____________________ Do not fear death so much, but rather the inadequate life.
Rogue
Member
Posts: 199 Registered: 31/12/1969 Status: Offline
posted on 6/1/2006 at 06:22 PM
You could also have defined a loop with a length of the length of the
string. Something like
myString =
for (int i=1; i
This is fairly common, needing to iterate over strings, arrays, hashes,
whatever, of unknown length.
Not sure if it works this way in Java (haven't coded it for a while), but
in Python empty objects evaluate to boolean False...this lets you do
something like
myArray=[1,2,3,4,5]
while myArray:
x=myArray.pop();
when it's empty, it stops without throwing an exception.
____________________ Plenty of time, my sweet. Plenty of time.
Psychopixi
Fanatic
Posts: 376 Registered: 31/12/1969 Status: Offline
posted on 7/1/2006 at 03:04 PM
Thanks for the advice Rogue, I kinda understood it, but I've got it working
the way I was hoping to using the toCharArray function.
____________________ Do not fear death so much, but rather the inadequate life.