2016-09-24 82 views
0

我正在製作一種將字符轉換爲相應數字的方法,例如a = 1,b = 2 ...Java字符數組錯誤

我一直在從IDE接收關於我的字典數組聲明的flak。我已閱讀文檔,但仍不知道如何改進。

感謝您提前給出的所有回覆! :d

編輯: '' 格式化

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 []字典是塊範圍,如何可以在外部訪問while語句 – mhasan

+0

我開始java ... –

回答

0

首先,你是不是初始化你的數組以外的if語句,這意味着你的代碼在最後將無法調用「dictionary」數組。其次,在你的場景中使用數組的問題是你有不同大小的數組。第三,就你如何初始化數組而言,正如人們已經指出的那樣,你需要爲你的數據集創建一個新的對象,例如, 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', 'A', '2', 'B', '3' , 'C', '4', 'd', '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語法。 –