Home > OS >  Getting a specific character in Java given two parameters
Getting a specific character in Java given two parameters

Time:09-26

I'm looking to find an uppercase letter associated with a given row and column of lower case letters in a java program. enter image description here

My specific question is how do you find the character at two indexes? I'm thinking of something like

UPPERCASE_LETTERS.indexOf.charAt(row, column);

But I know that's incorrect. Any suggestions? Thanks y'all

CodePudding user response:

You have rotated arrays of the english alphabet. You don't even need to declare the table shown in your example. You need to only add the column index to the row (which is column value - 97) and substract 26 if it is greater than z:

static char getResult(char row, char column){
    char c = (char)(row   column - 97);
    if(c > 'z') c = (char) (c - 26);
    return Character.toUpperCase(c);
}

CodePudding user response:

Suppose that we have a two-dimensional array named UPPERCASE_LETTERS. You archive your goal using the code as follow:

String charAt(char key, char message) {
    int row = key - 'a';
    int column = message - 'a';

    return UPPERCASE_LETTERS[row][column];
}
  • Related