Search code examples
javaarrayscharprimitive

Java Character Array Errors


I'm making a method for translating characters into their corresponding numbers e.g. a=1, b=2 ...

I've been recieving flak from the IDE about my declaration of the dictionary array. I've read the documentation and still have no idea how I would improve this.

Thanks for all your responses in advance! :D

EDIT: Formatting

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;
}

Solution

  • First of all, you are not initializing your arrays outside of the if statements, meaning your code at the end won't be able to call the "dictionary" array. Second of all, the problem with using arrays in your scenario that you have different sized arrays. Third of all, in terms of how you are initializing the arrays, as people have pointed out, you need to create a new object that is correct for your data set e.g. new char[] {...}.

    To fully solve your problem, you might want to consider something like this (I have initialized all of the arrays in the simplest way possible using the same method to avoid confusion):

    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;
    }