0

I'm doing something like

public static String[] list = {"a","b","c","d",}  //It gives me a NullPointeException if I didn't use static
public String encrypt(String a){
   a = a.replace(list[0],list[2]);
   a = a.replace(list[4],list[3]);
   return a;
}

and I have another method that just reverses it

public String decrypt(String a){
   a = a.replace(list[2],list[0]);
   a = a.replace(list[3],list[4]);
   return a;
}

Of course this is simplified, the real code I'm using uses the entire alphabet and some numbers. So here's my problem: If I input something like 123 into encrypt() and it outputs ngV then I input ngV into decrypt() it gives me like 1q3. Only some of the letters are correctly switched and some aren't. Is there something with the replace() method using array values that I'm missing? I'm obviously new to Java.

Also I read Java replace() problems but replaceAll() didn't work out.

Earlier, a moderator called Tunaki told me it was a duplicate, but I didn't find any other problems on stackoverflow that addressed this issue. If you guys can't figure out what the problem is from my sample above, I can post the actual code I have (it's the same thing, except the whole alphabet).

Community
  • 1
  • 1
Michael Chen
  • 21
  • 2
  • 5
  • 1
    Please edit your previous question and try to explain why it's not a duplicate to get it reopened. – Maroun Mar 03 '16 at 16:01
  • @Michael Chen Tunaki is not a moderator. – user3437460 Mar 03 '16 at 16:02
  • 1
    And please give example input/output that actually shows the problem behavior with the code you posted. – Bill the Lizard Mar 03 '16 at 16:03
  • Also please note that you totally changed your original question. You *didn't* assign the result returned by `replace`; which is why your question was marked as duplicate. – Maroun Mar 03 '16 at 16:04
  • I didn't assign the result returned by replace? I thought it always returned a string like http://www.tutorialspoint.com/java/java_string_replace.htm. – Michael Chen Mar 03 '16 at 16:19
  • Actually, I just figured out what my problem is, so I have a humongous list of .replace() but I didn't realize that...for example a.replace(b,c) and then later there would be a a.replace(c,d). I know how to fix this part. Thanks anyways – Michael Chen Mar 03 '16 at 16:20

0 Answers0