I am getting correct output for first for loop: for(int i=0;i<=name.length();i++)
but don't know why I am not getting any output for this loop: for(int i=name.length();i>=0;i--)
. While executing I am getting an error saying that index out of range.
I check the error here but I didn't understand it.
public class runner {
public static void main(String[] args) {
String name = "java";
System.out.println(".length method()" + name.length());// executing
// .length()
// method
System.out.println(".charAt method()" + name.charAt(5));
for (int i = 0; i <= name.length(); i++) {
System.out.println(name.charAt(i));
}
for (int j = name.length(); j >= 0; j--) {
System.out.println(name.charAt(j));
}
}
}
Output
j
a
v
a