private static String Encrypt(String strTarget) { // TODO Auto-generated method stub int len = strTarget.length()-1; String destination = ""; for (int i = 0; i<len; i++) { if (strTarget.charAt(i) != ' ') { char a = strTarget.charAt(i); int b = (int) a; b = strTarget.charAt(i)-4; a = (char) b; if ( b<70 && b>64) { b = strTarget.charAt(i)+26; a = (char) b; destination += a; } } return destination; }
I added a new line called destination , added characters to it, and returned it.
EDIT
However, it seems that you can change the actual String , strTarget .
To do this, you should not pass strTarget as your parameter, because it passes it by value, not by reference.
To actually change the string, pass a StringBuilder as described here . (By the way, here is a link.)
EDIT No. 2
Say you have a method. Call him foo . Foo takes an int as a parameter and sets it to 1:
public static void foo(int i) { i = 1; }
Now you want to test your method:
public static void main(String[] args) { int i = 0; foo(i); System.out.println(i); }
This should print 1, right? NO This is not true. It prints "0" . You can check it out if you want. This is because the integer is "passed by value". When something is passed by value, the virtual machine basically makes a copy and passes the copy to the method.
Therefore, when you execute i=1 in your foo method, you actually set the copy of i to 1 , not i . Thus, i remains unchanged.
There is another type of parameter passing called pass by reference. When something is passed by reference, the method changes the actual variable.
Arrays, for example, are passed by reference. Take this code, for example:
public static void foo(int[] i) { i[0] = 1;
In this example, i changes to foo because it is passed by reference.
In the original method, you return the return type as void . If the return type is not valid, you cannot return the encrypted String . So it occurred to me that perhaps you had to follow the link and change strTarget .
To do this, instead of passing strTarget you must pass a StringBuilder . You must create it using new StringBuilder(strTarget) , and it will automatically be pass by reference . If you wanted this, then I would like to create a destination String (as described above) and then change the StringBuilder to change strTarget to destination String .
Hope this helps.