You are using Strings in the wrong way (for Java!) lets clarify some basic points to use String's in Java:
String are immutable. This means each time you modify it JVM will create a new object. That is a lot of resources, so, in order of better programming you shouldn't use concatenation in Strings, to make concatenation use StringBuilder.
Strings does not end with any special symbol, this could happen in some filetypes but not at Strings objects, so you have to get size with length() and use it to iterate if necessary.
- Always take a loop at the API of any Java object to know its functionallities:
StringAPI7
StringAPI8
- To loop a
String char by char you can do it with for, while and several more ways (splitting, converting...):
For loop example:
for (int i = 0; i < str.length(); i++)
While example:
while (i < str.length()) {
Said that... Take a look at this code working and using what is explained:
public static void main(String[] args) {
String str = "Hello";
int i = 0;
// string builder is a mutable string! :)
StringBuilder copy = new StringBuilder();
// we iterate from i=0 to length of the string (in this case 4)
while (i < str.length()) {
// same than copy = copy + str.charAt(i)
// but not creating each time a new String object
copy.append(str.charAt(i));
// goto next char
i++;
}
// print result
System.out.println(copy);
}
UPDATE
thanks... but while I am trying this to find a reverse did not get result
If what you want is to reverse the String (your code didn't do that, you must write copy = str.charAt(i) + copy;) is much easier with StringBuilder. Take a look at this example:
public static void main(String[] args) {
String str = "Hello";
StringBuilder copy = new StringBuilder(str);
copy.reverse();
System.out.println(copy);
}