0

So here I have a piece of code that takes a string named key ("ALICE") and passes it into the method keyReader() to get the position of the alphabet of each index - to clarify A would be 1, L would be 12, I would be 9, C would be 3, and E would be 5. These numbers are stored into an array named keyArray[].

My problem is now using keyArray[] with these 5 elements that are stored in it and passing it into the method keyNumber() as a parameter in order change each number to base 27 and add it to a total which is keyNo in this case.

Any help and or suggestions are appreciated.

public class Problem2 {

    public static void main(String[] args) {

        String key = "ALICE"; //VARIABLE - Will be read from text file
        String cipherThis = "JLMRSULTQTXLRCQQEBCHQFWWE"; //VARIABLE - Will be read from text file

        int noKey = 0;
        int[] keyArray = new int[5];

        keyReader(key, keyArray); //reads the key
        keyNumber(noKey, keyArray); //evaluates the keyNumber of keyReader

    }

    //Method for reading each letter of the key
    private static void keyReader(String key, int[] keyArray) {
        for (int x = 0; x < 5; x++) {
            keyArray[x] = key.charAt(x) - 64;
        }
    }

    //Method for evaluating the key number 
    private static void keyNumber(int noKey, int[] keyArray) {
        int i = 0; //Counter for the numbers of the letters stored into the array using the keyReader() method
        int k = 4; //Counter for the 5 letters of the key (4,3,2,1,0)

        while (i < 5) {
            while (k >= 0) {
                noKey += Math.pow(27, k) * keyArray[i];
                k--;
                i++;
            }
        }
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • It seems your code is already passing `keyArray` just fine to `keyNumber`. Where's the problem? – mellamokb Mar 26 '14 at 04:16
  • See here: http://stackoverflow.com/questions/12757841/are-arrays-passed-by-value-or-passed-by-reference-in-java Easy workaround would be to have keyReader return an int array, and then call it like keyArray = keyReader(key, keyArray); And likewise for keyNumber, having it return a value you'd set noKey. – ryanlutgen Mar 26 '14 at 04:17
  • when the second method runs, keyArray[i] has all the elements as 0, thats the problem. so noKey gets returned as 0. – Kazz580 Mar 26 '14 at 04:18

3 Answers3

0

use Integer(Object Reference)array instead of int array (primitive). So u can get reference back and u can use it later for further processing.

Sagar Gandhi
  • 925
  • 6
  • 20
  • Yeah I just need to get the elements of my first array to be used as a parameter in my second method without making a global variable or 5 different ints (one for each element) as a parameter for the second method – Kazz580 Mar 26 '14 at 04:17
0

Instead of using...

private static void keyNumber(int noKey, int[] keyArray) {

Try using...

private static int keyNumber(int[] keyArray) {
    int noKey = 0;
    //...
    return noKey;
}

Which would be called using...

noKey = keyNumber(keyArray); //evaluates the keyNumber of keyReader

You should also consider doing the same thing with your keyReader, rather then passing it an array, have it return the result...

private static int[] keyReader(String key) {
    int[] keyArray = new int[key.length()];
    for (int x = 0; x < keyArray.length; x++) {
        keyArray[x] = key.charAt(x) - 64;
    }
    return keyArray;
}

And call it using...

int[] keyArray = keyArray = keyReader(key); //reads the key

You shouldn't rely on magic numbers, but work to known values...

Instead of...

int k = 4; //Counter for the 5 letters of the key (4,3,2,1,0)

while (i < 5) {
    while (k >= 0) {

You should be using...

int k = keyArray.length - 1; //Counter for the 5 letters of the key (4,3,2,1,0)

while (i < keyArray.length) {
    while (k >= 0) {

instead...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I'll give this a shot and let you know if it works tommorow, thanks for the suggestion – Kazz580 Mar 26 '14 at 04:21
  • Yeah, the magic numbers are actually static but I understand what you are saying – Kazz580 Mar 26 '14 at 04:28
  • 1
    I would also remove the static logic by putting your contents of main into a constructor then replacing the contents of main with Problem2 driver = new Problem2(); -> possibly passing a file and stuff as params depending on args. You'd then not have to add static to all your methods. – ryanlutgen Mar 26 '14 at 04:29
  • What happens when I give your methods a value that is either shorter then or greater then "5"? You can't rely on this been true, instead, you should be using the data you have at hand to make better decisions about what you can actually do... – MadProgrammer Mar 26 '14 at 04:29
0

In java all primitives, like ints are pass by value (ie. copied). What you want is to pass my reference (ie &). So Autobox the int using new Integer(someint) as an argument (should work) else its better to simply return the int (after the additions) from the keyNumber function.

public static void main (String [] args){

    String key = "ALICE"; //VARIABLE - Will be read from text file
    String cipherThis = "JLMRSULTQTXLRCQQEBCHQFWWE"; //VARIABLE - Will be read from text file

    int noKey = 0;
    int[] keyArray = new int[key.lenght];

    keyReader(key, keyArray); //reads the key
    keyNumber (new Integer(noKey), keyArray); //evaluates the keyNumber of keyReader

}

//Method for reading each letter of the key
private static void keyReader(String key, int[] keyArray) {
    for (int x = 0; x < keyArray.length; x++){
        keyArray[x] = key.charAt(x)-64;
    }
}

//Method for evaluating the key number 
private static void keyNumber(Integer noKey, int[] keyArray){
    int i = 0; //Counter for the numbers of the letters stored into the array using the keyReader() method
    int k = 4; //Counter for the 5 letters of the key (4,3,2,1,0)

    while (i < 5){
        while (k >= 0){
            noKey += Math.pow(27, k)*keyArray[i];
            k--;
            i++;
        }
    }   
}
ak_2050
  • 159
  • 4
  • And you run this code? `noKey += Math.pow(27, k)*keyArray[i];` doesn't look like a valid assignment for `Integer`... – MadProgrammer Mar 26 '14 at 04:31
  • Oh.. no I didn't at the time. I was just illustrating the point where you can pass by ref. There's a lot of things I would change in this code if I were to supply the whole file (ie. nested while loops are not needed, while (i < 5 && k >= 0) would suffice) since both variables are modified in the same loop (the nested loop). – ak_2050 Mar 26 '14 at 18:50