2016-09-24 84 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语法。 –