java - Splitting the string and store it in an array -


i want following string stored array throws arrayoutofbound exception.

public static void main(string[] args) {     // todo auto-generated method stub     char[] arr = {};     string str = "hello name ivkaran";     (int = 0;i < str.length(); i++){         system.out.println(str.charat(i));         arr[i] = str.charat(i);     } } 

you've declared array of length 0 line:

char[] arr = {}; 

to assign array, must initialized non-zero size. here, looks need same size string.

string str = "hello name ivkaran"; char[] arr = new char[str.length()]; 

you can call tochararray() on string char[] contents copied already.

char[] arr = str.tochararray(); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -