title: Array size issue for converting String to Unicode
body: <p>I have a string "text" in one class which calls on a method in another class to convert text in various ways. In this method though I am left with an "ArrayIndexOutOfBoundsException" error.</p>
  
  <pre><code>public String toUnicode() {
      char unicodeTextArray[] = new char[text.length()];
  
      if (text == null || text.isEmpty()) {
          return "";
      }
  
      String unicodeTextArrayString[] = new String[text.length()];
  
      for (int i = 0; i &lt; text.length(); i++) {
  
          unicodeTextArray[i] = text.charAt(i);
  
          if (unicodeTextArray[i] &lt; 0x10) {
              unicodeTextArrayString[i] = "\\u000" + Integer.toHexString(unicodeTextArray[i]);
          } else if (unicodeTextArray[i] &lt; 0x100) {
              unicodeTextArrayString[i] = "\\u00" + Integer.toHexString(unicodeTextArray[i]);
          } else if (unicodeTextArray[i] &lt; 0x1000) {
              unicodeTextArrayString[i] = "\\u0" + Integer.toHexString(unicodeTextArray[i]);
          }
          unicodeTextArrayString[i] = "\\u" + Integer.toHexString(unicodeTextArray[i]);
      }
  
      String unicode = unicodeTextArrayString[text.length()];
      return unicode;
  }
  </code></pre>
  
  <p>Changing one line to an arbitrarily large number such as:</p>
  
  <pre><code>String unicodeTextArrayString[] = new String[9999];
  </code></pre>
  
  <p>Results in no error, but it returns null. </p>
  
  <p>I thought about setting an int variable to increase the length of the array, but * 4 was still too small of an array size and it seems like if I go too large it just returns null.</p>
  
  <p>How could I go about getting the correct length of the array?</p>
  
  <p>EDIT: I found a non-array approach that works, but I would still like to know if there is a way to make the above array approach work in some way.</p>
  
  <pre><code>public String toUnicode() 
      {
          String unicodeString = "";
  
          for (int i = 0; i &lt; text.length(); i++) 
           { 
              char c = text.charAt(i);
              String s = String.format ("\\u%04x", (int)c);
              unicodeString = unicodeString + s;
           }
           return unicodeString;
      }
  </code></pre>
  
  <p>EDIT 2: In case anyone reading this is curious, to get the decimal value of the unicode:</p>
  
  <pre><code>    public String toUnicode() 
      {
          String unicodeString = "";
  
          for (int i = 0; i &lt; text.length(); i++) 
           { 
              char c = text.charAt(i);
              int unicodeDecimal = c;
              unicodeString = unicodeString + unicodeDecimal + " ";
           }
           return unicodeString;
      }
  </code></pre>
  
  <p>EDIT 3: I ended up deciding to use the following, which separates unicode decimals by space, and checks for the unicode value 10 (which means new line) and outputs a new line into the string instead of that value.</p>
  
  <pre><code>    public String toUnicode() 
      {
          String unicodeString = "";
  
          for (int i = 0; i &lt; text.length(); i++) 
           { 
              char c = text.charAt(i);
              int unicodeDecimal = c;  
  
              if (unicodeDecimal == 10)
              {
                  unicodeString = unicodeString + "\n";
              }
              else
              {
                  unicodeString = unicodeString + unicodeDecimal + " ";
              }
  
           }
           return unicodeString;
      }
  </code></pre>