-1

I've got problem. I want to do cipher in java, which does input number of steps from every char in string, based on ASCII table. Inscription should have only letters (big and small) and space (in coding and decoding too). This is my code, output doesn't print "z" and coding (first part, with true boolean) generates strange characters from unicode.

 static String Cezar(String tekst, boolean kodek, byte ile){
    char c;
    int i=0;
    int roboczy=0;
    String zwracany = new String();
    if(kodek==true){
    int odchyleniepo122, odchylenieprzed65=0;
    while(i<tekst.length()){
        //if (c<65)
            //c=65;
        c=tekst.charAt(i++);
        /*if (c>90 && c<97)
            c=97;
        if (c>122)
            c=122;
        if(c==32)
            c=65;*/
        roboczy = (int) c;
        roboczy = roboczy + ile;
        /*if(roboczy<0){
            roboczy=127;
        }
        if(roboczy>127){
            roboczy=0;
        }*/
        if (roboczy>90 && roboczy<97){

        }
        if (roboczy>122){
            odchyleniepo122=roboczy-122;
            //odchylenieprzed122=122-roboczy2;
            roboczy=65+odchyleniepo122;
        }
        if (roboczy<65 && roboczy!=32){
            odchylenieprzed65=65-roboczy;
        roboczy=122-odchylenieprzed65;
        }
        if (roboczy==32)
            roboczy=65;
        c=(char)roboczy;
        zwracany=zwracany+c;

                    }
    }
    if(kodek==false){
        int odchylenieprzed65 = 0;
    while(i<tekst.length()){
        //if (c<65)
            //c=65;
        c=tekst.charAt(i++);
        /*if (c>90 && c<97)
            c=97;
        if (c>122)
            c=122;
        if(c==32)
            c=65;*/
        roboczy = (int) c;
        //roboczy2=roboczy;
        roboczy = roboczy - ile;
        /*if(roboczy<0){
            roboczy=127;
        }
        if(roboczy>127){
            roboczy=0;
        }*/
        if (roboczy>90 && roboczy<97)
            roboczy=97;
        if (roboczy>122)
            roboczy=122;
        if(roboczy<65){
            odchylenieprzed65=65-roboczy;
            roboczy=122-odchylenieprzed65;
        }
        if(roboczy==65)
            roboczy=32;
        c=(char)roboczy;
        zwracany=zwracany+c;

                    }
    }
    return zwracany;
}

public static void main(String[] args) {
   String wynik1;
   byte ilosc = 126;
   wynik1=Cezar("la la la", true, ilosc);
   //wynik1=Cezar(wynik1, false, ilosc);
   System.out.println(wynik1);


}

}

  • 3
    Possible duplicate of [Java, How to implement a Shift Cipher (Caesar Cipher)](http://stackoverflow.com/questions/19108737/java-how-to-implement-a-shift-cipher-caesar-cipher) – CollinD May 10 '17 at 19:51
  • What happened when you tried debugging? – shmosel May 10 '17 at 19:52
  • ^ when i tried debugging, program just finished with output:±¦e±¦e±¦ - when i uncommented decoding function, it produces corecctly: lalala –  May 10 '17 at 19:55

1 Answers1

1

Better solution would be to define constant array of your chars available in your alphabet (or just a String as it is an implementation of char[]) e.g.:

String alphabet = "abcd...xyzABCD...XYZ " //don't forget the tailing space

then substitution of a specified character would be something like:

char substitutionChar = alphabet.charAt(alphabet.indexOf(charToSubstitute) + offset)

it affects performance as we process the alphabet String twice in just one substitution, but greatly increases readability and manageability of your code.

I'd also suggest you formatting your code. Just use some automated tool in your IDE it will really help you no matter how much you think it's useless :D

Jakub Licznerski
  • 1,008
  • 1
  • 17
  • 33