Is there a faster way to do this?

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
Code:
import java.io.*;
import javax.swing.*;

public class Main {
    public static void main(String[] args) {
       
        String[] loweralpha = new String[26];
        String[] upperalpha = new String[26];
        String[] numbers = new String[10];
        
        loweralpha[0] = "a";
        loweralpha[1] = "b";
        loweralpha[2] = "c";
        loweralpha[3] = "d";
        loweralpha[4] = "e";
        loweralpha[5] = "f";
        loweralpha[6] = "g";
        loweralpha[7] = "h";
        loweralpha[8] = "i";
        loweralpha[9] = "j";
        loweralpha[10] = "k";
        loweralpha[11] = "l";
        loweralpha[12] = "m";
        loweralpha[13] = "n";
        loweralpha[14] = "o";
        loweralpha[15] = "p";
        loweralpha[16] = "q";
        loweralpha[17] = "r";
        loweralpha[18] = "s";
        loweralpha[19] = "t";
        loweralpha[20] = "u";
        loweralpha[21] = "v";
        loweralpha[22] = "w";
        loweralpha[23] = "x";
        loweralpha[24] = "y";
        loweralpha[25] = "z";    
    }
}

Want to create 3 arrays, but surely there is a shorter way of writing this? Can't see how I could do it using a for loop.
 
Another option (although not sure it's possible) would be to use the ascii codes for the char's in a for loop:

for (i = 0; i < 26; i++)
loweralpha = (char) i + 97;

Guessing just casting as a char won't work though, but new String( i + 97) might, or there might be another way, i'm not overly good with Java but thats how i'd do it in C/C++
 
I don't know a lot about java, but would the below not work?
Code:
loweralpha[] = ["a","b",..."y","z"]

Almost, it just needs curly braces:

Code:
String[] loweralpha = {"a","b",..."y","z"};

Are you looking for the ability to check if a single letter is say upper case?

The java.lang.Character class provides methods for doing just these things: isLowerCase(char ch), isUpperCase(char ch), isDigit(char ch) and bunch of other useful helpers.
 
You could also try

char c = 'A';
int ascii = (int)c;

The ascii range would tell you if its a number/uppercase/lowercase/otherwise...

Think we're possibly getting a little away from what your after now?
 
Back
Top Bottom