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"
}
}