+ 2
What's wrong with the for loop, i need help
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); } int t=arr.length; for (int r = t; r >= 0; r-- ) { System.out.println( arr[r]) ; //your code goes here } }
3 Answers
+ 1
Moses Solomon Ayofemi
The } before int t = arr.length is causing your first problem.
Delete it from that position and place it with the other 2 at the base of code
}
}
}
Then you will be able to access your for loop, which is creating an index error
Adjust t-1
You might also wish to adjust your print statement
for(int r = t-1; r >= 0; r-- ){
System.out.print( arr[r]) ;
+ 4
You accidentally deleted a closing }
Plus, I think it should be
int t=arr.length-1;
as indexing starts from 0 and length != last index
+ 1
//} deleted
int t=arr.length-1;
for (int r = t; r >= 0; r-- ) {
System.out.println( arr[r]) ;
} //added
}
}