0

codePointCount() in java problem

https://code.sololearn.com/cw3uFPz49zmH/?ref=app According to ex. Why is the final(end) index 5 or does it not give an error in 5? While the string is of 5 characters. 0=H 1=e 2=l 3=l 4=o 5=? In copePointCount() method While the 5th index returns an error upon printing .In charAt() method

24th Oct 2019, 7:21 PM
ASP
ASP - avatar
4 Respuestas
+ 2
The syntax of codePointCount() is codePointCount(int beginIndex, int endIndex) Here if endIndex will be greater than size of String then it will give arrayIndexOfBoundException. But in your case endIndex is 5 which is equal to length of String that's why not giving exception. When you change to 6 then it gives exception. To know more check this :- https://www.w3resource.com/java-tutorial/string/string_codepointcount.php
24th Oct 2019, 7:46 PM
A͢J
A͢J - avatar
+ 1
ABHISHEK PATEL Because charAt depends on index which is start from 0. So in your case the last index is 4 but you are accessing 5th index. The index number starts from 0 and goes to n-1, where n is length of the string. It returns StringIndexOutOfBoundsException if given index number is greater than or equal to this string length or a negative number. Check this link. https://www.javatpoint.com/java-string-charat
24th Oct 2019, 7:55 PM
A͢J
A͢J - avatar
+ 1
Because the index AFTER the last Character throw an IndexOutOfBoundsException at the charAt method. Every method have his own Parameter and edges. Mean boundary conditions. for more detail refer the Documentation.
24th Oct 2019, 8:08 PM
Kiuziu 💘 Berlin
Kiuziu 💘 Berlin - avatar
0
But why charAt() method giving error. if length 5 according to you
24th Oct 2019, 7:52 PM
ASP
ASP - avatar