简体   繁体   中英

Java - Help converting letter to integer, adding 5, then converting back to letter

First off, here is my code so far

    public int encrypt() {
/* This method will apply a simple encrypted algorithm to the text.
 * Replace each character with the character that is five steps away from
 * it in the alphabet. For instance, 'A' becomes 'F', 'Y' becomes '~' and
 * so on. Builds a string with these new encrypted values and returns it.
 */

    text = toLower;
    encrypt = "";
    int eNum = 0;

    for (int i = 0; i <text.length(); i++) {
        c = text.charAt(i);
        if ((Character.isLetter(c))) {

       eNum = (int) - (int)'a' + 5;

        }  
    }


    return eNum;    
}

(text is the inputted string by the way. And toLower makes the string all lower case to make it easier converting.)

I got most of my assignment done, but one part of it is tasking me with moving every letter inputted 5 spaces over. A becomes F, B becomes G, etc.

So far from I got the letter converted to a number, but I am having trouble adding to it and then returning it back to a letter.

When I run the program and I enter my input such as "abc" I get '8'. It just adds them all up.

Any help would be much appreciated, and I can post the full code if necessary.

Few issues -

  1. First of all - eNum = (int) - (int)'a' + 5; you do not need the first (int) - i believe, you can just do - eNum = (int)c + 5; . Your expression would always result in a negative integer.

  2. Instead of returning eNum you should convert it to character and add it to a string and return the string at end (or you can create a character array of same length as string , keep storing the characters in the array, and return a string created from the character array).

  3. Instead of using a in the condition , you should use c which denotes the current character at the ith index.

  4. I am guessing not all of the variables in your code are member variables (instance variables) of the class , so you should define them with a datatype in your code.

Example changes to your code -

String text = toLower; //if toLower is not correct, use a correct variable to get the data to encrypt from.
        String encrypt = "";

    for (int i = 0; i <text.length(); i++) {
        char c = text.charAt(i);
        if ((Character.isLetter(c))) {

       encrypt += (char)((int)c + 5);

        }  
    }


   return encrypt;
//Just a quick conversion for testing
String yourInput = "AbC".toLowerCase();
String convertedString = "";

for (int i = 0; i <text.length(); i++) {
    char c = yourInput.charAt(i);
    int num = Character.getNumericValue(c);
    num = (num + 5)%128 //If you somehow manage to pass 127, to prevent errors, start at 0 again using modulus
    convertedString += Integer.toString(num);
}
System.out.println(convertedString);

Hope this is what you're looking for.

Try something like this, I believe this has several advantages:

public String encrypt(String in) {
    String workingCopy = in.toLowerCase();

    StringBuilder out = new StringBuilder();

    for (int i = 0; i < workingCopy.length(); i++) {
        char c = workingCopy.charAt(i);
        if ((Character.isLetter(c))) {
            out.append((char)(c + 5));
        }
    }

    return out.toString();
}

This code is a little bit verbose, but perhaps then it is easier to follow. I introduced the StringBuilder because it is more efficient than doing string = string + x

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