Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Auto increment" alphabet in Java?

"Auto increment" alphabet in Java - is this possible? From A to Z without a third-party library?

like image 709
Dacto Avatar asked Jan 12 '10 06:01

Dacto


People also ask

How do you represent a to z in Java?

In Java, the char variable stores the ASCII value of a character (number between 0 and 127) rather than the character itself. The ASCII value of lowercase alphabets are from 97 to 122. And, the ASCII value of uppercase alphabets are from 65 to 90. That is, alphabet a is stored as 97 and alphabet z is stored as 122.

What is a method that can be used to increment letters?

Basically it increments letters like the column ID's of an Excel spreadsheet. nextChar('yz'); // returns "ZA" function nextChar(c) { var u = c. toUpperCase(); if (same(u,'Z')){ var txt = ''; var i = u. length; while (i--) { txt += 'A'; } return (txt+'A'); } else { var p = ""; var q = ""; if(u.

Can we increment string in Java?

You can't. Strings are immutable.


3 Answers

Yes, you can do it like this:

for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
    System.out.println(alphabet);
}

It is also possible with typecasting:

for (int i = 65; i <= 90; i++) {
    System.out.println((char)i);
}
like image 97
Richie Avatar answered Sep 22 '22 16:09

Richie


Yes, like this:

for (int i = 0; i < 26; i++)
{
    char upper = (char) ('A' + i);
    char lower = (char) ('a' + i);
    ...
}
like image 18
Taylor Leese Avatar answered Sep 21 '22 16:09

Taylor Leese


for (char c = 'A'; c <= 'Z'; c++) {
  ...
}
like image 9
Laurence Gonsalves Avatar answered Sep 24 '22 16:09

Laurence Gonsalves