2016-09-24 24 views
0

文字を対応する数字に変換する方法を作成しています。 a = 1、b = 2 ...Java文字配列エラー

ディクショナリ配列の宣言についてIDEからflakを受け取りました。私はドキュメントを読んだが、これをどのように改善するかはまだ分かっていない。

事前にお返事いただきありがとうございます。 :D

EDIT: ''

public static int charNumTrans(char toBeTranslated){ 
    //Variable init 
    int translated = 0; 
    char guessedVariable; 

    //Checking if between a and i 
    if(toBeTranslated == 'a' || toBeTranslated == 'b' || toBeTranslated == 'c' || toBeTranslated == 'd' || toBeTranslated == 'e' 
    || toBeTranslated == 'f' || toBeTranslated == 'g' || toBeTranslated == 'h' || toBeTranslated == 'i'){ //Checking to see which array to use 

     char[] dictionary; 
     dictionary = new char {'0','.','1','a','2','b','3','c','4','d','5','e','6','f','7','g','8','h','9','i'}; 

     //chekcing between j and s 
    }else if(toBeTranslated == 'j' ||toBeTranslated == 'k' ||toBeTranslated == 'l' ||toBeTranslated == 'm' ||toBeTranslated == 'n' || 
      toBeTranslated == 'o' ||toBeTranslated == 'p' ||toBeTranslated == 'q' ||toBeTranslated == 'r' || toBeTranslated == 's'){//Checking to see if in between 
     dictionary[10] = {'0','j','1','k','2','l','3','m','4','n','5','o','6','p','7','q','8','r','9','s'}; 
    }else{//Everything else will be in this data set. 
     char[] dictionary = {'0','t','1','u','2','v','3','w','4','x','5','y','6','z'}; 
    } 

    guessedVariable = dictionary[1]; 
    while(dictionary[guessedVariable] != toBeTranslated){ 
     guessedVariable +=2; 
     } 

    // Assigns letter minus one of array. e.g. b = dictionary[5]. This will then assign dictionary[4] to translated. 
    translated = Character.getNumericValue(dictionary[guessedVariable-1]); 
    return translated; 
} 
+0

あなたのchar:

は完全にあなたの問題を解決するために、あなたはこのような何か(私は混乱を避けるために、同じ方法を使用して最も簡単な方法で可能配列の全てを初期化している)を検討する必要があります辞書はブロックスコープですが、どのようにして文の中で外部からアクセスできるのですか – mhasan

+0

私はJavaを始めました... –

答えて

0

まず、あなたがありませんif文の外で配列を初期化します。つまり、最後のコードは "辞書"配列を呼び出すことができません。第二に、異なるサイズの配列を持つシナリオで配列を使用する際の問題点です。第3に、配列を初期化する方法に関して、人々が指摘したように、データセットに適した新しいオブジェクトを作成する必要があります。 new char[] {...}。 ] [

public static int charNumTrans(char toBeTranslated){ 
    //Variable init 
    int translated = 0; 
    char guessedVariable; 

    //Checking if between a and i 
    if(toBeTranslated == 'a' || toBeTranslated == 'b' || toBeTranslated == 'c' || toBeTranslated == 'd' || toBeTranslated == 'e' 
|| toBeTranslated == 'f' || toBeTranslated == 'g' || toBeTranslated == 'h' || toBeTranslated == 'i'){ //Checking to see which array to use 

     char[] dictionary = {'0','.','1','a','2','b','3','c','4','d','5','e','6','f','7','g','8','h','9','i'}; 
     return findValue(dictionary); 

    //checking between j and s 
    }else if(toBeTranslated == 'j' ||toBeTranslated == 'k' ||toBeTranslated == 'l' ||toBeTranslated == 'm' ||toBeTranslated == 'n' || 
     toBeTranslated == 'o' ||toBeTranslated == 'p' ||toBeTranslated == 'q' ||toBeTranslated == 'r' || toBeTranslated == 's'){//Checking to see if in between 

     char[] dictionary = {'0','j','1','k','2','l','3','m','4','n','5','o','6','p','7','q','8','r','9','s'}; 
     return findValue(dictionary); 

    }else{//Everything else will be in this data set. 

     char[] dictionary = {'0','t','1','u','2','v','3','w','4','x','5','y','6','z'}; 
     return findValue(dictionary); 

    } 

} 

public static int findValue(char[] dictionary){ 
    guessedVariable = dictionary[1]; 
    while(dictionary[guessedVariable] != toBeTranslated){ 
     guessedVariable +=2; 
    } 

    // Assigns letter minus one of array. e.g. b = dictionary[5]. This will then assign dictionary[4] to translated. 
    translated = Character.getNumericValue(dictionary[guessedVariable-1]); 
    return translated; 
} 
0

変更を 辞書=新しいチャー{ '0'、 '1'、 ''、 '2'、 'B'、書式 '3' 5 '、' e '、' 6 '、' f '、' 7 '、' g '、' 8 '、' h '、' 9 '、'私'};あなたの配列の宣言については

dictionary = new char[] {'0','.','1','a','2','b','3','c','4','d','5','e','6','f','7','g','8','h','9','i'}; 
0

はあなたが作成している配列オブジェクトに値を代入するとき、あなたは[]が不足している - そう、例えば、これにあなたの宣言を変更します。

char[] dictionary = new char[]{'0','t','1','u','2','v','3','w','4','x','5','y','6','z'}; 

また、数字に相当する数値に文字を変換しようとしている場合、関数に渡したcharintにキャストし、この値から61を引くことをお勧めしますか? 理由ビーイング61は文字「」Unicode文字テーブル上の位置に対応し、大幅にお渡し文字に番号を割り当てる単純化することである。全ての

+0

ありがとうございました!私は半分のC++と半分のJavaシンタックスをプログラミングしていたと思います。 –