0

I copy and pasted some code from my eclipse project into my android studio java file. I fixed up any variable declaration to fit android studio, but my code does not perform the same. In particular my for loop:

 int z = 0;
 for (int i = 0;i < translatable.length;i++){
      translatable[i] = Chars[z] + Chars[z+1];
      z+=2;
 }

when used in eclipse, if i enter matthew it will return:

ma tt he wz

but when used in my android studio application it returns:

m at th ew

*i have made a playfair cipher which i am trying to convert to an android app.

to add more context i will add the previous code also, in which i think works fine but just in case someone is able to pick up on something im not:

encrypt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String[][] key = new String[5][5];
            String[] filler =
                    {
                            "a","b","c","d","e",
                            "f","g","h","i","k",
                            "l","m","n","o","p",
                            "q","r","s","t","u",
                            "v","w","x","y","z"
                    };


            String txt = text.getText().toString();


            String[] words = txt.split(" ");

            int alphNum = 0;
            String joinTxt ="";

            for (int i = 0; i < words.length;i++){

                if(words[i].length() % 2 != 0){

                    words[i] = words[i] + "z";

                    alphNum = alphNum + words[i].length();
                    joinTxt = joinTxt + words[i];


                } else {

                    alphNum = alphNum + words[i].length();
                    joinTxt = joinTxt + words[i];

                }
            }

            String[] Chars = joinTxt.split("");

            String[] translatable = new String[Chars.length / 2];

            int z = 0;
            for (int i = 0;i < translatable.length;i++){
                translatable[i] = Chars[z] + Chars[z+1];
                z+=2;
            }

Im finding it hard to understand why I am encountering this problem and do not understand the logic. If someone could point me in the right direction that would be great as the exact same code works fine in another IDE. Sorry about my naming conventions.

EDIT: after doing some more debugging, i found i was wrong in thinking my for loop was the problem for this.

Each split statement used has left a leading empty array item, and using the necessary methods i cant seem to get rid of them. i have managed to work around it by indexing from 1 instead of 0. I will do more research into the split method and why this is happening. Thank you for your help.

Matt
  • 36
  • 5
  • 3
    *I fixed up any variable declaration to fit android studio,* that makes no sense. You should not have to change anything – Tim Jan 24 '17 at 09:25
  • sorry, what i meant was i had to change the charsequence from my textedit to string @TimCastelijns – Matt Jan 24 '17 at 09:30
  • i will try this @Eran and no i have tested numerous times, the only way it would be like that is if the textedit begins with a space my default – Matt Jan 24 '17 at 09:32
  • are you compiling both project with the same java version? – Alessandro Verona Jan 24 '17 at 09:34
  • @Eran i still get the same results with the added .trim unfortunately – Matt Jan 24 '17 at 09:34
  • Why don't you add print statements of the various arrays (words, Chars, translatable). That would make it very easy to find where the problem is. – Eran Jan 24 '17 at 09:50
  • i removed them for the post to try and make the code more readable @Eran the everything else produced the right results. – Matt Jan 24 '17 at 09:53
  • @Saret to be completely honest im not sure how to check – Matt Jan 24 '17 at 09:53
  • @saret it seems android studio is using JRE 1.8.0_76, while eclipse is using JRE 1.8.0_121 i will look into it further and see if i can match them up *i cant seem to find the information for the JDK – Matt Jan 24 '17 at 10:56

1 Answers1

1

Use

split(" ",-1)

instead of

split(" ")

read that:

Why in Java 8 split sometimes removes empty strings at start of result array?

and

Behaviour of String.split in java 1.6?

Community
  • 1
  • 1
Alessandro Verona
  • 1,157
  • 9
  • 23