Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I was trying to copy a word using this manner. I am not sure,I was following right manner handling String.

The code is:

 public static void main(String args[])
   {
      String str="Hello";
      int i=0;
      String copy = "";
      while (str.charAt(i) !='')
      {
          copy = copy + str.charAt(i);
          i++;
      }

      System.out.println(copy);
   }

Running this code results in Exception:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
    at java.lang.String.charAt(Unknown Source)
    at ReverseWord.main(ReverseWord.java:15)

Am I using charAt() and checking null in right way? Or,I have wrong conception about String handling in Java?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
155 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...