0

Okay so, obviously I'm new with Java and what I currently wish to do is a very simple program to encrypt a string by breaking it into an array of characters and replacing the characters with new ones.

So what I did so far was to create a key-array containing the alphabet, which I'm comparing the split-up string with, and I'm trying to replace the characters with a value-array which is basically just the alphabet backwards.

My code so far works when I'm just printing out the value, but it wont properly replace the characters.

public class Main {

    public static void main(String[] args) {

        char[] keyArray =  {'a', 'b', 'c', 'd',  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        char[] valueArray = {'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'p', 'q', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd',  'c', 'b', 'a'};

        String myString = "abcxyz";
        char[] myStringArray = myString.toCharArray();

        for(int x = 0; x<myString.length(); x++)
        {
            for(int i = 0; i<keyArray.length; i++)
            {
                if(myStringArray[x] == keyArray[i])
                {
                    //System.out.println(valueArray[i]); would give the output "zyxcba" as expected 
                    myStringArray[x] = valueArray[i]; // this will only change the characters in the first half of keyArray 
                }
            }
        }

        System.out.println(myStringArray); //Outputs "abccba" instead of "zyxcba"
    }
}
1

2 Answers 2

3

The problem you have is that you continue to loop through the key array even after you've already made the replacement - allowing it to replace it a second time!

You'll need to 'break' out of the for loop once you've done your replacement.

public class Main {

    public static void main(String[] args) {

        char[] keyArray =  {'a', 'b', 'c', 'd',  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        char[] valueArray = {'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'p', 'q', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd',  'c', 'b', 'a'};

        String myString = "abcxyz";
        char[] myStringArray = myString.toCharArray();

        for(int x = 0; x<myString.length(); x++)
        {
            for(int i = 0; i<keyArray.length; i++)
            {
                if(myStringArray[x] == keyArray[i])
                {
                    //System.out.println(valueArray[i]); would give the output "zyxcba" as expected 
                    myStringArray[x] = valueArray[i]; // this will only change the characters in the first half of keyArray 
                    break; //Exit the loop checking against the keyArray
                }
            }
        }

        System.out.println(myStringArray); //Outputs "abccba" instead of "zyxcba"
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Aaah, I understand! Thank you so much, everything works as expected now :)
0

public class Main {

public static void main(String[] args) {

    char[] keyArray =  {'a', 'b', 'c', 'd',  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    char[] valueArray = {'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'p', 'q', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd',  'c', 'b', 'a'};

    String myString = "abcxyz";
    char[] myStringArray = myString.toCharArray();

    for(int x = 0; x<myString.length(); x++)
    {
        for(int i = 0; i<keyArray.length; i++)
        {
            if(myStringArray[x] == keyArray[i])
            {
                //System.out.println(valueArray[i]); would give the output "zyxcba" as expected 
                myStringArray[x] = valueArray[i]; // this will only change the characters in the first half of keyArray
                break; //Introduced break so that when the answer is set, break and move to the next iteration of myStringArray
            }
        }
    }


    System.out.println(myStringArray); //Outputs "abccba" instead of "zyxcba"
}

}

I introduced break in between to control when the item is matched.

There are many other ways of making this optimal though. But the main thing is you should not make x*i comparison when it is not required.

4 Comments

I will also add one more thing that instead of converting your string into array you can do the operation using string methods charAt or substring.
Thank you for your answer! Yeah I've yet to explore the various options of doing it, this is my first program in Java. I will take a look at the string operations for sure! But what do you mean with the x*i comparison? Are you saying I should be using something else than for-loops?
By x * i, I mean that the total number of cases for this will be myString.length() * keyArray.length() which is not a efficient way to solve the problem, in this following case you are obtaining your result well before end of the inner loop.
Ah I see what you mean. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.