Похожие чаты

This algorithm shows the position of the letter in the

alphabet. How to do the opposite when there is a number, and you need a letter?

public static void main(String args[]) {

String s = "HUYES";
String t = "";
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
if (Character.isLetter(ch)) { // check if the character is a letter
if (!t.isEmpty()) {
t += " ";
}
int n = (int) ch - (int) 'A'; // if you want to check, then add 'A'+ 1. Where the beginning of the alphabet begins with 1
t += String.valueOf(n);
}
}
System.out.println(t);
}

6 ответов

21 просмотр

Get ascii code of 'A', add the number and then use the ascii code to convert to char char c = ((int)'A' + num)

Manav | avoid unnecessary messaging me
^ this can be a source of errors. So make sure you...

i just need to have a number from 0-25 and convert to alphabetical order and get the letter

holy moly, that's so ... cryptic. I would never understood what this code is doing without extra description. Why not just do it more straightforward? class EnglishAlphabet { static String alphabet = "abcdefghijklmnopqrstuvwxyz"; int positionInAlphabet(char letter) { return alphabet.indexOf(letter); } Character letterAtPosition(int pos) { if (0 <= pos && pos < alphabet.length()) { return alphabet.charAt(pos); } else { return null; // or throw if you want to be NPE-free } } } More readable. More maintainable - just imagine that some day comes PO and says "Ok, now we need to support Spanish and French".

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта