Closed Thread Icon

Topic awaiting preservation: Incrementing letters in JAVA (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8949" title="Pages that link to Topic awaiting preservation: Incrementing letters in JAVA (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Incrementing letters in JAVA <span class="small">(Page 1 of 1)</span>\

 
Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 11-19-2003 09:00

If I take something like this, I get it to increment A > Z just fine but then it starts over with A instead of moving to AA then AB, AC, etc like most programming languages do.

code:
char x;
for ( char x = 'A'; x <= 'Z'; x++ )
System.out.print(x);
System.out.println();



Is there a way to achieve that Z > AA > AB > AC with JAVA? Or am I asking for something outside it's scope?

Synthetic's Chess Player Page

[This message has been edited by Synthetic (edited 11-19-2003).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 11-19-2003 09:31

This is a JavaScript forum, not a Java forum.

quote:
I get it to increment A > Z just fine but then it starts over with A instead of moving to AA then AB, AC, etc like most programming languages do.



I've never heard of a programming language that does that. After Z, I believe, would come the next character in the ASCII character set. Especially with a 'char' variable, you're not going to get a two-character string.

Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 11-19-2003 10:25

Well there wasn't a forum for JAVA and this was as close as I could get

Anyway lots of languages let you do this, for example PHP with something as simple as this would go a,b,c,d all the way to z then would go aa, ab, ac, to az then to ba, bb, bc, etc...

I probably am explaing it badly but try this in php and you would see what i mean...

code:
$letter = 'a'; 
for ($a = 1; $a <= 26; $a++) {
$foo[] = $letter++;
}


You can also do this in perl, asp, and many more. I was just looking for something like that in JAVA

*Feel free to move if you think there is a better forum*

[This message has been edited by Synthetic (edited 11-19-2003).]

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 11-19-2003 12:34

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

rickindy
Nervous Wreck (II) Inmate

From: Indianapolis, In USA
Insane since: Jan 2002

posted posted 11-19-2003 13:54

You'll need to start with en empty string for a base.
As you go through your loop, tack the character from the loop onto the string.
As you ccomplete each iteration of the alphabet, you need to change the base string to reflect how many times you've gone through the alphabet (A=1,B=2,etc) and then tack the character from the loop onto that string.
Not very graceful, but it will work

[This message has been edited by rickindy (edited 11-19-2003).]

MajorFracas
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2003

posted posted 11-20-2003 16:18

Why not just use an int for your counter and convert to string representation as needed?
e.g.: convert(26) == "aa"
convert(702) == "aaa"

Try the recursive function below. I've included main() to demonstrate the rollover at key values.

code:
public class Test {

String convert(int value) {
char charVal = (char) ('a' + value % 26); // convert remainder to value between a - z

int div = value / 26;
if (div > 0) {
return convert(div - 1) + charVal; // subtract 1 from div so we start over at 'a' and not 'b'
}
else {
return "" + charVal; // empty string in front so charVal gets converted to string (quick & dirty)
}
}

public static void main(String[] args) {
Test t = new Test();

for ( int i = 24; i < 28; i++) {
System.out.println(i + " = " + t.convert(i));
}

System.out.println();

for ( int i = 26*26 + 24; i < 26*26 + 28; i++) {
System.out.println(i + " = " + t.convert(i));
}

System.out.println();

for ( int i = 26*26*26 + 26*26 + 24; i < 26*26*26 + 26*26 + 28; i++) {
System.out.println(i + " = " + t.convert(i));
}

}
}




« BackwardsOnwards »

Show Forum Drop Down Menu