Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

How to create an array containing a range of characters in Java

1 Answer

0 votes
import java.util.Arrays;

public class CharacterRangeArray {
    public static char[] CreateCharacterRangeArray(char startChar, char endChar) {
        if (endChar < startChar) {
            throw new IllegalArgumentException("End character must be greater than or equal to start character.");
        }

        char[] charArray = new char[endChar - startChar + 1];
        for (int i = 0; i <= endChar - startChar; i++) {
            charArray[i] = (char) (startChar + i);
        }
        
        return charArray;
    }

    public static void main(String[] args) {
        // Create an array of lowercase characters
        char[] charArray = CreateCharacterRangeArray('a', 'm');
        System.out.println(Arrays.toString(charArray));

        // Create an array of uppercase characters
        char[] upperCaseArray = CreateCharacterRangeArray('A', 'M');
        System.out.println(Arrays.toString(upperCaseArray));
    }
}



/*
run:

[a, b, c, d, e, f, g, h, i, j, k, l, m]
[A, B, C, D, E, F, G, H, I, J, K, L, M]

*/

 



answered Mar 21, 2025 by avibootz
...