Массив английского алфавита java

for loop, iteration through alphabet? java

I can iterate through the alphabet, but I’m trying to keep the last iteration and add on the next letter. this is my code.

for(char alphabet = 'a'; alphabet

I want it to print out something that looks like this. a ab abc abcd abcde. and so forth. How is this possible?

6 Answers 6

You need to add the char alphabet to a string.

String output = ""; for(char alphabet = 'a'; alphabet

Never use String += String in a loop. Use StringBuilder . Sure, this is a small inconsequential loop, but start doing it the right way, so it becomes second nature.

i edited like 2 seconds after i posted lol! such fast response time haha. One thing to note. the output += alphabet needs to be inside the print statement System.out.println(output += alphabet);

@pewpew Of all the answers, you accept the one with the worst performance of them all. It is a bit simpler to write than the rest, I’ll give you that. It’s a good thing there’s only 26 letters in the alphabet.

I will go with StringBuffer or StringBuilder. Something like:

StringBuffer sb = new StringBuffer(); for (char alphabet = 'a'; alphabet
StringBuilder sb = new StringBuilder(); for (char alphabet = 'a'; alphabet

String vs StringBuffer vs StringBuilder:

String: It is immutable, so when you do any modification in the string, it will create new instance and will eatup memory too fast.

StringBuffer: You can use it to create dynamic String and at the sametime only 1 object will be there so very less memory will be used. It is synchronized (which makes it slower).

StringBuilder: It is similar to StringBuffer. The olny difference is: it not synchronized and hence faster.

So, better choice would be StringBuilder. Read more.

StringBuilder sb = new StringBuilder(); IntStream.range('a', 'z').forEach(i -> < sb.append((char) i); System.out.println(sb.toString()); >); 

Источник

Better way to generate array of all letters in the alphabet

Note that the above somewhat artificially depends on the fact that all lower-case Roman letters in ASCII/Unicode are contiguous. It would not work, eg, with EBCDIC.

Читайте также:  Как в тор выключить javascript

17 Answers 17

I think that this ends up a little cleaner, you don’t have to deal with the subtraction and indexing:

char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray(); 

Ah, figured there might’ve been a cleaner way to do it without typing everything out or loops. 🙁 I suppose I’ll go with this one. Thanks!

@HunterMcMillen Java source files are Unicode (so, in a string literal, that’s what you have and that’s all you can add).

@Thilo possibly, but not all users are likely to use the same alphabet, so then you are in the tricky situation of «Do we store all alphabets as constants?» or «Can we even do that reasonably since some alphabets are very large?»

char[] LowerCaseAlphabet = ; char[] UpperCaseAlphabet = ; 

This getAlphabet method uses a similar technique as the one described this the question to generate alphabets for arbitrary languages.

Define any languages an enum, and call getAlphabet .

char[] armenianAlphabet = getAlphabet(LocaleLanguage.ARMENIAN); char[] russianAlphabet = getAlphabet(LocaleLanguage.RUSSIAN); // get uppercase alphabet char[] currentAlphabet = getAlphabet(true); System.out.println(armenianAlphabet); System.out.println(russianAlphabet); System.out.println(currentAlphabet); 

Result

I/System.out: աբգդեզէըթժիլխծկհձղճմյնշոչպջռսվտրցւփքօֆ

I/System.out: абвгдежзийклмнопрстуфхцчшщъыьэюя

I/System.out: ABCDEFGHIJKLMNOPQRSTUVWXYZ

private char[] getAlphabet() < return getAlphabet(false); >private char[] getAlphabet(boolean flagToUpperCase) < Locale locale = getResources().getConfiguration().locale; LocaleLanguage language = LocaleLanguage.getLocalLanguage(locale); return getAlphabet(language, flagToUpperCase); >private char[] getAlphabet(LocaleLanguage localeLanguage, boolean flagToUpperCase) < if (localeLanguage == null) localeLanguage = LocaleLanguage.ENGLISH; char firstLetter = localeLanguage.getFirstLetter(); char lastLetter = localeLanguage.getLastLetter(); int alphabetSize = lastLetter - firstLetter + 1; char[] alphabet = new char[alphabetSize]; for (int index = 0; index < alphabetSize; index++) < alphabet[index] = (char) (index + firstLetter); >if (flagToUpperCase) < alphabet = new String(alphabet).toUpperCase().toCharArray(); >return alphabet; > private enum LocaleLanguage < ARMENIAN(new Locale("hy"), 'ա', 'ֆ'), RUSSIAN(new Locale("ru"), 'а','я'), ENGLISH(new Locale("en"), 'a','z'); private final Locale mLocale; private final char mFirstLetter; private final char mLastLetter; LocaleLanguage(Locale locale, char firstLetter, char lastLetter) < this.mLocale = locale; this.mFirstLetter = firstLetter; this.mLastLetter = lastLetter; >public Locale getLocale() < return mLocale; >public char getFirstLetter() < return mFirstLetter; >public char getLastLetter() < return mLastLetter; >public String getDisplayLanguage() < return getLocale().getDisplayLanguage(); >public String getDisplayLanguage(LocaleLanguage locale) < return getLocale().getDisplayLanguage(locale.getLocale()); >@Nullable public static LocaleLanguage getLocalLanguage(Locale locale) < if (locale == null) return LocaleLanguage.ENGLISH; for (LocaleLanguage localeLanguage : LocaleLanguage.values()) < if (localeLanguage.getLocale().getLanguage().equals(locale.getLanguage())) return localeLanguage; >return null; > > 

Источник

How to get a range of characters? (alphabet)

I have been working on this for hours and now Im kinda stuck. please help me. Im a complete programming handicap. All the methods work fine except the alphabet one. It will receive two characters (either upper or lower case) and return a string composed of the range of char values given. Maintain the same case (upper or lower) that was passed in to the method. If an upper case and a lower case char (one of each) was passed to the method, convert the upper case char into lower case and use the lower case range. Note, the range will be inclusive of the starting char and exclusive of the ending char. Also, observe that if the starting (first) char given is greater than the ending (second) char, for example ‘m’ and ‘h’, then the method will return an empty string since there are no chars in this range. Can you give me some help on how I can do the above on the alphabet method?

import java.util.*; class CharacterOperations < public static void run() < int number=1; Scanner scanner = new Scanner(System.in); while(number >0) < System.out.println("(1) Insert 1 to change a letter from its lower case value to its upper case value"); System.out.println("(2) Insert 2 to change a letter from its upper case value to its lower case value "); System.out.println("(3) Insert 3 for the alphabet method (range of two letters) "); System.out.println("Enter a number (or negative to quit): "); number = scanner.nextInt(); if (number == 1) < System.out.print("Enter a lower case letter: "); String a= scanner.next(); char letter = (char) a.charAt(0); toUpper(letter); >else if (number == 2) < System.out.print("Enter an upper case letter: "); String a= scanner.next(); char letter = (char) a.charAt(0); toLower(letter); >else if (number == 3) < System.out.print("Enter an upper case or lower case letter: "); System.out.print("Enter an upper case or lower case letter: "); String a= scanner.next(); char letter1 = (char) a.charAt(0); String b= scanner.next(); char letter2 = (char) b.charAt(0); alphabet(letter1, letter2); >> > public static char toUpper(char letter) < int rep = ((int)letter - 32); char ltr = (char)rep; System.out.println("The letter "+ ltr + " integer representation is: " + rep); return (char) ((int) letter -32); >public static char toLower(char letter) < int rep = (int)(letter + 32); char ltr = (char)rep; System.out.println("The letter " + ltr + " integer representation is: " + rep); return (char) ((int) letter + 32); >public static String alphabet( char letter1, char letter2) < int rep1 = (int)letter1; int rep2 = (int)letter2; char ltr1 = (char)rep1; char ltr2 = (char)rep2; System.out.println("The letter " + ltr1 + " integer representation is: " + rep1); System.out.println("The letter " + ltr2 + " integer representation is: " + rep2); >> 

7 Answers 7

With a char you can just ++ it to get the next char and so on.

char a = 'a'; a++; // now you have b a++; // now you have c 

Just do a while loop to go from start to end char.

Читайте также:  Vk api method users get python

Use this to first to generate a random letter. You first have to generate a random number within a certain range. Then when you get the number back and store it in a char variable, it shows it as a letter.

char letter = 0; letter = (char) (65 + (char)(Math.random() * (90 - 65) + 1)); 

This answaer assumes you are just talking about standard keyboard characters from the ASCII set.

Take the ascii codes for the 2 charaters and create a loop:

StringBuilder buf = new StringBuilder(); for (int i = rep1; i  

This will work as they both need to be same case.

Источник

Create Alphabet List from list : Java

and from this list i would like to create a Alphabet list like if there are files name start with A then add A to list and like so upto Z and in the same way i want to create Numbers List also from 0 to 9 All these Alphabet and Number checking is based on Starts with from the file name . how to do this in java. Finally the AlphabetList will be like A B E F. Z and the Numbers list will be 1 2 3 9 thanks

3 Answers 3

Similarly you can add logic for number as well

 List alphabetList = new ArrayList(); int alpha = (int)fileNameList.charAt(0); while(alpha

After initializing AlphabetList :

You can do a similar thing for NumbersList .

occurrences = new int[256]; for (fileName: fileNameList) < occurrences[(int)fileName.charAt(0)] += 1; >for (int i = 'A', i 0) < AlphabetList.add((char)i) >> for (int i = '0', i 0) < NumberList.add(i - '0') >> 

Great and thanks . it gives me what i want and thanks it worked is there any better way of doing this instead of inner loop ? like recursion

You can use a HashMap> to store your lists with a character key. This could serve the same use for numbers and letters but you could create 2 maps if you for some reason want them separated.

private HashMap> alphabetList = new HashMap<>(); public void addFileName(String fileName) < if(fileName.isEmpty()) < return; >Character firstChar = Character.toUpperCase(fileName.charAt(0)); List storedList = alphabetList.get(firstChar); if(storedList == null) < storedList = new ArrayList(); alphabetList.put(firstChar, storedList); > storedList.add(fileName); > 

Источник

Оцените статью