0

EDIT2: Guess the question goes here. So my first question is, am I scanning the file correctly (probably not)?

So I am trying to build an encoder in Java using a Vigenere cipher. The key and plain text files are suppose to be passed in the command line with the execution of the program (like ">java vigenere k1.txt pt1.txt"). Then its suppose to scan the file for the text (which is one of the parts I don't think I did right) where it will remove all non-alphabetic letters and convert all of the text to lower text, for each (key and plain text), and pad the plain text with 'x' if its shorter than the key text when repeated. From there its suppose to encrypt the plain text by shifting the characters by the key characters % 26.

import java.util.Scanner;

public class vigenere {

    //string length constant
    public static final int MAX_LENGTH = 512;    

    public static void main(String[] args) {
        //obtaining file names
        String keyFile = args[0];
        String plainTextFile = args [1];

        //scanning files
        Scanner keyContents = new Scanner(keyFile);
        keyContents.useDelimiter("//Z");
        Scanner plainTextContents = new Scanner(plainTextFile);
        plainTextContents.useDelimiter("//Z");

        //turn scanners into strings
        String key = keyContents.next();
        String plainText = plainTextContents.next();

        //conver string to stringbuilder
        StringBuilder key1 = new StringBuilder(key);
        StringBuilder plainText1 = new StringBuilder(plainText);

        //set length of strings
        key1.setLength(MAX_LENGTH);
        plainText1.setLength(MAX_LENGTH);

        //set stringbuilder back to string
        key = key1.toString();
        plainText = plainText1.toString();

        System.out.println("Original Key:");
        System.out.println(key);
        System.out.println("****************************");
        System.out.println("Original PlainText:");
        System.out.println(plainText);
        System.out.println("****************************");

        encrypt(plainText, key);
    }

    static void encrypt(String plainText, String key) {
        //new strings
        String newKey = "";
        String newPlainText = "";

        //convert to all lowercase
        key = key.toLowerCase();
        plainText = plainText.toLowerCase();

        for(int i=0; i<key.length(); i++) {
            char currentKey = key.charAt(i);
            if(currentKey < 'a' || currentKey > 'z')
                continue;
            newKey += (char)(currentKey);
        }

        System.out.println(newKey);

        for(int i=0, j=0; i<plainText.length(); i++) {
            char currentPlainText = plainText.charAt(i);
            if(currentPlainText < 'a' || currentPlainText > 'z')
                continue;
            newPlainText += (char)((currentPlainText + newKey.charAt(j) % 26) + 'A');
        }

        System.out.println(newPlainText);
    }
}

The outputs currently are:

Original Key:
k1.txt
****************************
Original PlainText:
p1a.txt
****************************
ktxt
´¥¸¼¸
  • 1
    [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – shmosel Mar 08 '18 at 02:02
  • Added my first question. – Anthony Pionessa Mar 08 '18 at 02:17
  • 1
    Please edit your question and remove the first 4 paragraphs. Replace it with your actual question. State simply what your program is meant to do (1 sentence), what it does currently, what you expect it to do, and where you think you may have gone wrong. If you haven't done any debugging find a tutorial on debugging java with breakpoints or just add simple print statements to trace your code. – scrappedcola Mar 08 '18 at 02:27
  • Moved it to the top and got rid of the non-useful stuff. I already had print statements which is where the outputs come from. – Anthony Pionessa Mar 08 '18 at 03:03
  • Cool that's much easier to follow, thank you. Now for what I meant by print statements goes beyond just he output. When debugging your code you can use print statements throughout to check the value of variables as you go. Ideally you would use a debugger to check variable states, but when first starting out print statements work just as well. So to start put a print after each variable to show the contents of that variable and see if it matches your expectations. This can lead to why your output doesn't match up. – scrappedcola Mar 08 '18 at 03:09
  • And yes I do realize this doesn't answer your first question, but should help with future. You might try opening your file before trying to read it. – scrappedcola Mar 08 '18 at 03:11

0 Answers0