如何阻止键盘返回大写字母java

时间:2016-04-26 04:37:19

标签: java

我有一个键盘,它返回在用户输入键盘上制作字符串所需的按键次数,但我应该只计算没有任何大写或标点符号的字符串。我怎样才能将标点符号或带有大写字母的字符串排除在其中?

public class Keypad

char [][] letters; 


public Keypad(String chars, int rowLength)
{
    int column = chars.length()/rowLength;
    if(chars.length()%rowLength!=0) {
        column++;
    }

    letters = new char[column][rowLength]; 
    for(int i = 0, n=0; i < letters.length ; i++) {
        for(int j = 0 ; n < chars.length() && j < letters[i].length ; j++, n++) {
            letters[i][j] = chars.charAt(n);
        }
    }

}



public int[]  find(char letter) {
    int [] coordinates;
    coordinates = new int [2];
    for(int i=0; i<letters.length; i++) {
        for(int j = 0; j<letters[i].length; j++) {

            if(letters[i][j] != letter){
                coordinates[0] = -1; 
                coordinates[1] = -1; 
            }
            else {
                coordinates[0]= i; 
                coordinates[1]= j;  
                return coordinates; 
            }
        }

    }
    return coordinates; 
}


public int pressesRequired(String wordWanted) {
    int total = 0; 
    char searching; 
    int [] nextCoordinates = new int [2];
    int [] currentCoordinates = new int[] {0, 0};

    for(int i=0; i<wordWanted.length(); i++) {
        searching = wordWanted.charAt(i);
        nextCoordinates = find(searching); 
        total += Math.abs(currentCoordinates[0]-nextCoordinates[0]); 
        total += Math.abs(currentCoordinates[1]-nextCoordinates[1]); 
        currentCoordinates = nextCoordinates; 
        total++; 


    }

    return total; 
}

1 个答案:

答案 0 :(得分:0)

使用String.toLowerCase()和平等:

public int pressesRequired(String wordWanted) {
    if (!wordWanted.equals(wordWanted.toLowerCase())) {
        return -1;   // -1 indicates an error
    }

    // otherwise proceed as you were
}
相关问题