3
    for (int j = 0; j <= l - 1; j++) 
   { char c1 = str.charAt(j);
      n[j] = c1;
   }
//It is showing error-arrayIndexoutofbounds

'str' is an inputted string and 'l' is the length of the string str. I have to store all its character in an array and the array 'n' is of char type I have tried to store it by for loop but its showing error . Please help me out. Just remove that equal to sign in ur loop it will only be less than

5
  • 2
    How did you declare n? Commented Nov 24, 2016 at 14:45
  • 1
    can you post full code Commented Nov 24, 2016 at 14:45
  • please provie an minimal reproducible example Commented Nov 24, 2016 at 14:47
  • array have a fixed length that is determined when you create it, that exception means that you're trying to put too many elements. check the size of n. Commented Nov 24, 2016 at 14:50
  • @Mritunjay I have tried to post the full code but it was showing the problem of indentation . I tried to correct it but could not so posted this part only .Can you please tell me how to post the full code. Commented Nov 24, 2016 at 14:53

3 Answers 3

5

Your array n should be declared as:

char[] n = new char[str.length()];

That creates an array with the exact size needed to put all your String's characters in it.

An ArrayIndexOutOfBoundsException is thrown when you access an illegal index of the array, in this case I suspect your array is smaller than the length of the String.

Sign up to request clarification or add additional context in comments.

2 Comments

Or, alternatively, char[] n = new char[l]. (<- that's a lower-case L, not the digit "one")
Please note: Strings in Java are UTF-16 Unicode. Because Unicode contains more than 65536 characters, this means some characters require 2 16-bit symbols to encode. For this reason, Oracle recommends against using char types to represent character arrays (yes it's too bad but Unicode outgrew 16-bit only after Java already had comitted to it). Instead, make an array of int and use codePointAt and codePointCount etc.
4

No need to create and fill such an array manually, there is a built-in method String.toCharArray that does the job for you:

n = str.toCharArray()

3 Comments

No worries. You can replace the whole for-loop with the one line from my answer - it does the same thing.
how can I post the full code it has some other errors also(not syntax) that I want to get clear
You can edit your original question by clicking on "edit" (right under your question), but perhaps consider just writing a new question if you have other issues.
2

If you want to convert string into character array then string class already have method called toCharArray().

String str = "StackOverflow";
char[] charArray = str.toCharArray();

then if you want to check content of array print it.

for(int i = 0;i < charArray.length ; i++) {
     System.out.print(" " + charArray[i]);
}

2 Comments

To print an array, it's preferable to use the following piece of code: System.out.println(Arrays.toString(charArray));
@Olivier Agreed. Other than that, this answer is exactly the same as mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.