2016-04-26 6 views
0

ユーザ入力キーボードで文字列を作るために必要なキーの数を返すキーボードがありますが、それは持っていない文字列を計算するだけです大文字または句読点。どのように句読点や大文字の文字列を除外することができますか?キーパッドが大文字で返されないようにする方法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; 
} 

答えて

0

使用String.toLowerCase()と平等:

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

    // otherwise proceed as you were 
} 
関連する問題