How do you input a Array's of ints and chars t

2020-05-03 10:53发布

问题:

I have two classes that do two different things. I am trying to get my FileAccess class to use my encryption class to to encode a set number of phrases in a text file. The first 10 numbers in the file give the program the key value and that should be stored as a int and what comes after the file should be stored as a array of char and those need to be called by the encryption class to code the phrase. I do not know why I can't call my encryption class and I am stumped.

Sorry for being unclear I am trying to design an code that will accept a number of phrases as input and allow the user to encrypt it through the use of an encryption key. This key should be made up of an integer number between-2000000 and +2000000.The encryption algorithm uses the key to shift the letters of the alphabet to the right or left.For example A encoded with a key of 3 would produce D three letters to its right in the alphabet. If the key is so large that the new letter goes past the end of the alphabet, the program should wrap around to a letter near the beginning of the alphabet.

The FileAccess class – This class should read several phrases from a file. The first line of the file should contain an integer indicating the number of phrases in the file. The first 10 characters of each phrase in the file should contain the encryption key to be used in the encryption and decryption process for that phrase. This class should provide a way to access this information by other classes. Finally, this class should have a second method to allow phrases to be saved into a new file. I tried to be as clear as i can now. My problem is I cant call my encode method in my encryption class Here is the code for the File Access.

public class FileAccess {
    public static String[] load(String fileName) throws IOException {
        FileReader file = new FileReader(fileName); //open file for reading
        BufferedReader input = new BufferedReader(file);

        int sizeF = Integer.parseInt(input.readLine()); // variable for the size of the array
        String infoInFile[] = new String[sizeF]; // declare and create a string array

        for (int i = 0; i < sizeF; i++) { // loop to read the file into the array
            infoInFile[i] = input.readLine();
        }
        input.close();//close the file 
        return infoInFile;
    }

    public static int[] key(String finalKey[]) {
        int finaloutput[] = new int[5];
        String temp;

        for (int i = 0; i < finalKey.length; i++) {
            temp = finalKey[i].substring(0, 11);
            finaloutput[i] = Integer.parseInt(temp);
            System.out.println(finaloutput[i]);

        }

        return finaloutput;
    }

    public static char[] phrase(String EndOfPhrase[]) {
        char letter[] = new char[5];

        for (int j = 0; j < EndOfPhrase.length; j++) {
            String phrase;
            phrase = EndOfPhrase[j].substring(11);
            char temp = phrase.charAt(1);
            letter = phrase.toCharArray();
            System.out.println(letter);
        }

        return letter;
    }

    public static void main(String[] args) throws IOException {
        String output[]; // call the loader
        int[] keyTest;
        char[] phraseTest;
        String display;

        output = FileAccess.load("phrase.txt");
        keyTest = key(output);
        phraseTest = phrase(output);

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

I am not sure if I should have that for loop, but scice the encryption encode method only takes in 1 char at a time and codes I think I need a for loop to keep calling it

HERE IS THE CODE FOR THE encryption code

 public class Encryption {

    public static boolean isNotALetter(char character) { // returns false if the character is a letter
        boolean yorn = false;

        return yorn;
    }

    public static char encode(char letter, int key) { // returns an encrypted character
        char encryptedcharacter = 0;
        int truevalueofkey = 0;
        int valueofletter;
        int newvalueofletter;

        valueofletter = Encryption.lettertovalue(letter);

        truevalueofkey = key % 26;

        newvalueofletter = (valueofletter + truevalueofkey)%26;

        encryptedcharacter = Encryption.valueToLetter(newvalueofletter);

        // add truevalueofkey to key to get

        return encryptedcharacter;
    }

    public static char decode(char letter, int key) { // returns a decrypted character
        char decodedcharacter = 0;
        int dtruevalueofkey = 0;
        int dvalueofletter;
        int dnewvalueofletter;

        dvalueofletter = Encryption.lettertovalue(letter);

        dtruevalueofkey = key % 26;

        dnewvalueofletter = (dvalueofletter - dtruevalueofkey)%26;

        decodedcharacter = Encryption.valueToLetter(dnewvalueofletter);

        return decodedcharacter;
    }

    public static int lettertovalue(char letter) { // get value of each letter ex A = 1
        int value = 0;

        // convert to string based on char
        switch (letter) {
        case 'a': {
            value = 1;
            break;
        }
        case 'b': {
            value = 2;
            break;
        }
        case 'c': {
            value = 3;
            break;
        }
        case 'd': {
            value = 4;
            break;
        }
        case 'e': {
            value = 5;
            break;
        }
        case 'f': {
            value = 6;
            break;
        }
        case 'g': {
            value = 7;
            break;
        }
        case 'h': {
            value = 8;
            break;
        }
        case 'i': {
            value = 9;
            break;
        }
        case 'j': {
            value = 10;
            break;
        }
        case 'k': {
            value = 11;
            break;
        }
        case 'l': {
            value = 12;
            break;
        }
        case 'm': {
            value = 13;
            break;
        }
        case 'n': {
            value = 14;
            break;
        }
        case 'o': {
            value = 15;
            break;
        }
        case 'p': {
            value = 16;
            break;
        }
        case 'q': {
            value = 17;
            break;
        }
        case 'r': {
            value = 18;
            break;
        }
        case 's': {
            value = 19;
            break;
        }
        case 't': {
            value = 20;
            break;
        }
        case 'u': {
            value = 21;
            break;
        }
        case 'v': {
            value = 22;
            break;
        }
        case 'w': {
            value = 23;
            break;
        }
        case 'x': {
            value = 24;
            break;
        }
        case 'y': {
            value = 25;
            break;
        }
        case 'z': {
            value = 26;
            break;
        }
        }
        return value;
    }

    public static char valueToLetter(int value) {

        char letter = 0;

        if (value == 1) {
            letter = 'a';
        }
        if (value == 2) {
            letter = 'b';
        }
        if (value == 3) {
            letter = 'c';
        }
        if (value == 4) {
            letter = 'd';
        }
        if (value == 5) {
            letter = 'e';
        }
        if (value == 6) {
            letter = 'f';
        }
        if (value == 7) {
            letter = 'g';
        }
        if (value == 8) {
            letter = 'h';
        }
        if (value == 9) {
            letter = 'i';
        }
        if (value == 10) {
            letter = 'j';
        }
        if (value == 11) {
            letter = 'k';
        }
        if (value == 12) {
            letter = 'l';
        }
        if (value == 13) {
            letter = 'm';
        }
        if (value == 14) {
            letter = 'n';
        }
        if (value == 15) {
            letter = 'o';
        }
        if (value == 16) {
            letter = 'p';
        }
        if (value == 17) {
            letter = 'q';
        }
        if (value == 18) {
            letter = 'r';
        }
        if (value == 19) {
            letter = 's';
        }
        if (value == 20) {
            letter = 't';
        }
        if (value == 21) {
            letter = 'u';
        }
        if (value == 22) {
            letter = 'v';
        }
        if (value == 23) {
            letter = 'w';
        }
        if (value == 24) {
            letter = 'x';
        }
        if (value == 25) {
            letter = 'w';
        }
        if (value == 26) {
            letter = 'z';
        }

        return letter;

    }

    public static void main(String[] args) {

        String yrn = "y";

        while (yrn  == "y") {
            String alsdjkf = JOptionPane.showInputDialog(null, "Enter the letter");
            char enchar = alsdjkf.charAt(0);
            int keyr = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the key"));
            char newchar = Encryption.decode(enchar, keyr);
            JOptionPane.showMessageDialog(null, newchar);
            yrn = JOptionPane.showInputDialog(null, "yes or no");
        }

    }

}

This is what is in the text file:

2
00000000003 The cook worked 12 hours in the darkened kitchen!

00000000025 Did Fred look well? That’s it!

回答1:

Unfortunately it's quite hard to tell from your question what you are trying to do. I think you want each line to be interpreted as 10 digits and then a phrase to be encoded by the key represented by the digits. Assuming that's correct, I have several suggestions for changes to your code. I recommend you try these and then come back if they don't solve your problem.

  1. FileAccess.load is unnecessary. You can use Files.lines to get all lines in a file in a single statement (use Stream.toArray if you really need it to be in an array).

  2. The massive switch statements to just turn char to int are not needed. You can do math on char values such as letter - 'a' to simplify these.

  3. Use a regular expression rather than decoding each line yourself. "(\\d{10}) (.*)" will read the key and phrase in a single statement.

  4. Once you have the key and phrase you can call your "encryption" code for each line.

And just a warning: if you come back and say "I'm not allowed to use X or Y in my answer" then my comment will be "that would have been useful to know before I put time into trying to help you"!