+ 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 } }

4th Dec 2021, 9:52 PM
Moses Solomon Ayofemi
Moses Solomon Ayofemi - avatar
3 odpowiedzi
+ 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]) ;
4th Dec 2021, 10:33 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 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
4th Dec 2021, 10:20 PM
Lisa
Lisa - avatar
+ 1
//} deleted int t=arr.length-1; for (int r = t; r >= 0; r-- ) { System.out.println( arr[r]) ; } //added } }
4th Dec 2021, 10:35 PM
zemiak