简体   繁体   中英

“Crypting” Exercise in Java

I am doing an exercise in which I need to replace the letters from a String to another letter (the 4th letter in front of it in the Alphabet. I also have to eliminate any character that is not a letter except spaces (" "). So far, I am able to do the first step (eliminating the characters), but I fail at replacing the letters. Here is my code, the last part of it is definitely not allowing me to replace the letters properly, do you have an idea of what I've done wrong??

Thanks in advance

import java.util.Scanner;

public class Crypto {

    static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
    static final int DECALAGE = 4;

    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        System.out.println("Veuillez entrer une chaine de caracteres : ");
        String s = scanner.nextLine();

        // la chaine a coder
        String aCoder = "";
        // la chaine codee
        String chaineCodee = "";


char [] ALPHA = new char [26];

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

{
        ALPHA[i] = ALPHABET.charAt(i); 
}

    for (int j = 0; j < s.length(); j++)
    {
        char a = s.charAt(j);
        if (Character.isLetter(a) || a==' ')
        {
            aCoder += a;
        }
        aCoder.length();

for(int i =0; i<aCoder.length(); i++)
{
    char f = aCoder.charAt(i);
    if (Character.isLetter(f))
            {
        int aide = ALPHABET.indexOf(aCoder.charAt(i));
        if (aide < 21)
        {char ajout = ALPHABET.charAt(aide + DECALAGE);
        chaineCodee = chaineCodee + ajout;
        }
        else { 
            char ajout2 = ALPHABET.charAt(aide - 22);
            chaineCodee = chaineCodee + ajout2;
        }
            }
    else{
        chaineCodee = chaineCodee + " ";
    }
}
    }

        System.out.format("La chaine initiale etait : '%s'\n", s);

        if (aCoder.isEmpty()) {
            System.out.println("La chaine a coder est vide.\n");
        } else {
            System.out.format("La chaine a coder est : '%s'\n", aCoder);
            System.out.format("La chaine codee est : '%s'\n", chaineCodee);
        }
    }
}

Your problem is in the way you nested your for-loops.

Each time through your outer loop, you set the next character of aCoder , and then iterate through aCoder and encode it in its entirety, giving you a sort of 'twelve-days-of-christmas' output when you build chaineCodee .

When I use your program to encode "fnord" , instead of "jrsvh" , I'm getting "jjrjrsjrsvjrsvh" ( "j" + "jr" + "jrs" + "jrsv" + "jrsvh" ).

Instead of nesting your loops like this:

    for (int j = 0; j < s.length(); j++) {
        char a = s.charAt(j);
        if (Character.isLetter(a) || a == ' ') {
            aCoder += a;
        }
        for (int i = 0; i < aCoder.length(); i++) {
            char f = aCoder.charAt(i);
            if (Character.isLetter(f)) {
                int aide = ALPHABET.indexOf(aCoder.charAt(i));
                if (aide < 21) {
                    char ajout = ALPHABET.charAt(aide + DECALAGE);
                    chaineCodee = chaineCodee + ajout;
                } else {
                    char ajout2 = ALPHABET.charAt(aide - 22);
                    chaineCodee = chaineCodee + ajout2;
                }
            } else {
                chaineCodee = chaineCodee + " ";
            }
        }
    }

Just move the inner loop outside, and encode the source string once it's done being built:

    for (int j = 0; j < s.length(); j++) {
        char a = s.charAt(j);
        if (Character.isLetter(a) || a == ' ') {
            aCoder += a;
        }
    }
    for (int i = 0; i < aCoder.length(); i++) {
        char f = aCoder.charAt(i);
        if (Character.isLetter(f)) {
            int aide = ALPHABET.indexOf(aCoder.charAt(i));
            if (aide < 21) {
                char ajout = ALPHABET.charAt(aide + DECALAGE);
                chaineCodee = chaineCodee + ajout;
            } else {
                char ajout2 = ALPHABET.charAt(aide - 22);
                chaineCodee = chaineCodee + ajout2;
            }
        } else {
            chaineCodee = chaineCodee + " ";
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM