Reputation: 1462
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
Upvotes: 1
Views: 2897
Reputation: 589
Always remember to check the highest index that an array can have. In your case the name.length()
function returns the size of the String name.
Suppose your String is "JAVA" it contains 4 characters. So name.length()
will return 4.
But notice that the indices for each character are:
'J' - 0
'A' - 1
'V' - 2
'A' - 3
When you put your index counter at 4 it tries to access something that does not exist in the bounds of the array. Hence the ArrayIndexOutOfBoundsException
is raised.
For solving your problem make all name.length()
calls into name.length() - 1
. Or modify the for loop to not include the case when the counter equals the name.length()
Upvotes: 0
Reputation: 5055
The other answers already pointed out that indexing begins at zero in java (and most other programming languages).
Since you seem to be new to java, here an example without the explicit use of indices.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test
{
public static void main(String[] args)
{
String name = "java";
System.out.println("NORMAL ORDER\n");
for (char c : name.toCharArray()) // for each loop
System.out.println(c);
System.out.println("\nREVERSE ORDER\n");
List<Character> chars = new ArrayList<Character>(); // it's not possible to use primitive types as generic parameter
for (char c : name.toCharArray()) // we fill the chars list
chars.add(c); // autoboxing from char to Character
Collections.reverse(chars);
for (char c : chars)
System.out.println(c);
}
}
OUTPUT
NORMAL ORDER
j
a
v
a
REVERSE ORDER
a
v
a
j
Upvotes: 1
Reputation: 5217
You have out of bound exception, because Java array indexing starts from zero. For example if you have array with length = 5, then index of the first element will be 0 and index of the last element will be 4 (length - 1).
Change your line
for(int i=0;i<=name.length();i++){
to
for(int i=0;i<name.length();i++){
and line
for(int j=name.length();j>=0;j--){
to
for(int j=name.length()-1;j>=0;j--){
Upvotes: 2
Reputation: 31184
arrays are 0-indexed, change <=
to <
your will get that error every time you call
name.charAt(name.length())
Upvotes: 2
Reputation: 31648
The problem is i<=name.length();
you want i<name.length();
because the length goes beyond the bounds of the wrapped char array in String.
For the same reason, you need to change the second for loop to
for(int j=name.length()-1 ;j>=0;j--){
Upvotes: 6