0

I'm trying to implement a simple cesar cipher. I'm having trouble with the function that codes a string based on a new key[]. Here I shown, how I setup my key.

private String[] alphabet =  {"a", "b", "c","d", "e", "f","g", "h", "i","j", "k", "l","m", "n", "o",
            "p", "q", "r","s", "t", "u","v", "w", "x","u", "z"};

public String[] CoderNewKey(int shift) {
    _NewKey = null;
    _NewKey = new String[25];

    for (int i = 0; i < 25; i++) {
        _NewKey[i] = alphabet[(i + shift) % 25];
    }
    return _NewKey;
}

The problem I have is with Coder, none of the if statements are ever true, and rtnstring is always just "".

public String Coder(String[] key, String msg){
    String rtnstring = "";

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

        for (int x = 0; x<26 ; x++){
            if ( msg.substring(i, i+1) == alphabet[x]){

                rtnstring.concat(key[x]);
            }if (msg.substring(i, i+1) == " "){

                rtnstring.concat(" ");
            }
        }
    }
    return rtnstring;
}

Here is my logic for the coder method.

It goes through the String msg 1 letter at a time. It matches the letter with its index in the alphabet, then it creates a new string based on the index's in the given key.

K DawG
  • 13,287
  • 9
  • 35
  • 66
Zaid90
  • 81
  • 1
  • 11
  • In java, Strings should always be compared with equals method. You will find a good explanation here.http://www.thejavageek.com/2013/07/27/string-comparison-with-equals-and-assignment-operator/ – Prasad Kharkar Dec 31 '13 at 06:58
  • 2
    this kind of question asked about 1-5 times per day. and the answer is keep flooding – Baby Dec 31 '13 at 06:59
  • @RafaEl This question has the interesting twist, though, that the best answer is "don't use `String`". – chrylis -cautiouslyoptimistic- Dec 31 '13 at 07:02
  • 2
    @chrylis I think most of us agree with that. but i'm getting angry to see _"for string comparision, use equals(), equalsIgnoreCase()"_ kind of answers :-/ – Baby Dec 31 '13 at 07:09

6 Answers6

9

You want to use char instead of String for this; char values are individual characters, which is what you're working with. To compare String objects, you need to use equals(), but you can use == with chars.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
5

You need to use equals() method from the String class. When you use == you are comparing the reference variable of two objects (which is not same in your code)

  +----------+                        +----------+
  |          |     What you need      |          |
  |  "   " <-------to compare-----------> "a"    |
  |          |                        |          |
  |          |                        |          |
  +----------+                        +----------+
       ^                                   ^
       |                                   |
       +              What you             +
   rtnstring <------+ erroneously +---->  msg
                      compare
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
2

First of all instead of substring use the function charAt and then change the String[] to char[] i.e.,

private char[] alphabet =  {'a', 'b', .... , 'z'};

and

if ( msg.charAt(i) == alphabet[x])

In your example you are using String and when you use == then it compares the references and substring function will always return a new string for avoiding such problems use equals(String) instead .

To provide better understanding read the Java Documentation The example will provide you all the details how String are initialized and what is meant by references equality.

Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
1

String should be compared using equals() method, not ==. equals() method is used to check the String literals are meaningfully equivalent or not

public String Coder(String[] key, String msg){
    String rtnstring = "";

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

        for (int x = 0; x<26 ; x++){
            if ( msg.substring(i, i+1).equals(alphabet[x])){

                rtnstring.concat(key[x]);
            }if (msg.substring(i, i+1).equals(" ")){

                rtnstring.concat(" ");
            }
        }
    }
    return rtnstring;
}
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
1
if ( msg.substring(i, i+1).equalIgnoreCase(alphabet[x])){
    rtnstring.concat(key[x]);
}
if (msg.substring(i, i+1).equalIgnoreCase(" ")){
    rtnstring.concat(" ");
}

Your comparison should use equalIgnoreCase or equal in case of strings but if you want to just compare characters

// first define array of characters
private char[] alphabet =  {'a', 'b', ..... 'z'};
// then you can use ==
if ( msg.charAt(i)== alphabet[x]){
    rtnstring.concat(key[x]);
}
if (msg.charAt(i) == '\b')){
    rtnstring.concat(" ");
}
Tareq Salah
  • 3,720
  • 4
  • 34
  • 48
0

private Char[] alphabet = {'a', 'b', 'c','d', 'e', 'f','g', 'h', 'i','j', 'k', 'l','m', 'n', 'o','p', 'q', 'r','s', 't', 'u','v', 'w', 'x','u', 'z'};

I turned your String into char(characters) for you. You can continue to use ==if you choose to change to char.

Richard
  • 1
  • 3