-1

I've got a simple caesar cipher program that takes input from a text file based on the same parameters. The problem I have is that when I use an input string with spaces, when my loop is going through the alphabet variable, it does not consider the space in the input file. What I would Like to Know Is If There Is A Way The Program Can't Consider The Space And If It Finds A Space In The Input File, It Will Pass To The Next Letter. down is my code

public static int shiftnumber = 0;
public static int EncrDecrpt = 0;

public static String plainText = "";
public static String encryptStr = "";
public static int languageInteger = 0;
public static boolean turkish = false;
public static boolean spanish = false;
public static boolean english = false;


public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    File inputFile = new File("filename.txt");
    Scanner scan = new Scanner(inputFile);

    while (scan.hasNext()) {

        String line = scan.nextLine();
        String[] words = line.split(":");

        shiftnumber = Integer.parseInt(words[0]);
        EncrDecrpt = Integer.parseInt(words[1]);
        languageInteger = Integer.parseInt(words[2]);
        plainText = words[3].toLowerCase();
        
        System.out.println(line);
        // System.out.println(secondParameter);

    }
    encyption(shiftnumber, EncrDecrpt, plainText, encryptStr, ALPHABET);

public static String encyption(int shiftnumber, int EncrDecrpt, String plainText, String encryptStr,
        String ALPHABET) {
    int encryptPos = 0;
    for (int i = 0; i < plainText.length(); i++) {
        // get position of each character of inputStr in ALPHABET
        int pos = ALPHABET.indexOf(plainText.charAt(i));

        // get encrypted char for each char of inputStr

        encryptPos = (shiftnumber + pos) % 29;
        
        
        char encryptChar = ALPHABET.charAt(encryptPos);

        // add encrypted char to encrypted string
        encryptStr += encryptChar;

    }
    System.out.println(encryptStr);
    return encryptStr;

}

in the input file i have hello world with a shift 4 i get the output lippsdasvh instead of LIPPS ASVPH

Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63

1 Answers1

0

When the input string contains spaces, indexOf return -1

This causes the encryption loop to skip the space character entirely, an produce incorrect output

I propose to modify the encryption func

public static String encyption(int shiftnumber, int EncrDecrpt, String plainText, String encryptStr,
        String ALPHABET) {
    int encryptPos = 0;
    for (int i = 0; i < plainText.length(); i++) {
        char c = plainText.charAt(i);
        if (c == ' ') {
            // If the current character is a space, skip it and continue to the next one
            encryptStr += " ";
            continue;
        }
        // get position of each character of inputStr in ALPHABET
        int pos = ALPHABET.indexOf(c);

        // get encrypted char for each char of inputStr

        encryptPos = (shiftnumber + pos) % 29;
        
        
        char encryptChar = ALPHABET.charAt(encryptPos);

        // add encrypted char to encrypted string
        encryptStr += encryptChar;

    }
    System.out.println(encryptStr);
    return encryptStr;
}
InvstIA
  • 84
  • 4