M
markspace
I'm doing some text parsing and I'd appreciate some input on code styles
for loops.
A lot of the processing I'm currently doing involves finding a
particular character, and then doing something with the text up to that
point. For example:
int i;
for( i = 1; Character.isLetter( s.charAt( i) ); i++ )
{}
// do something with i and s here
This marches ahead until it find a character that isn't a "letter",
then, with i set to the offset of the last letter+1, is able to do
something with that group of letters in string s.
My question is on the code style I used here. The for loop looks a
little funny. I think it's the best because all the terms (is that
right? terms = the bits between the semicolons) of the for statement do
the right thing and are in the standard position.
However, the body is empty, which is weird, and i has to be declared
outside of the for loop so I can use it later, which is a little weird.
Any thoughts on how to format this? Would a while loop be better or
more readable here?
int i = 1;
while( Character.isLetter( s.charAt( i ) ) ) {
i++;
}
That while loop wasn't tested, but should do the same thing as the for
loop above. Any ideas?
for loops.
A lot of the processing I'm currently doing involves finding a
particular character, and then doing something with the text up to that
point. For example:
int i;
for( i = 1; Character.isLetter( s.charAt( i) ); i++ )
{}
// do something with i and s here
This marches ahead until it find a character that isn't a "letter",
then, with i set to the offset of the last letter+1, is able to do
something with that group of letters in string s.
My question is on the code style I used here. The for loop looks a
little funny. I think it's the best because all the terms (is that
right? terms = the bits between the semicolons) of the for statement do
the right thing and are in the standard position.
However, the body is empty, which is weird, and i has to be declared
outside of the for loop so I can use it later, which is a little weird.
Any thoughts on how to format this? Would a while loop be better or
more readable here?
int i = 1;
while( Character.isLetter( s.charAt( i ) ) ) {
i++;
}
That while loop wasn't tested, but should do the same thing as the for
loop above. Any ideas?