0

Can someone pls explain whats wrong with this code?it shows the result,but still wrong.

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(); //your code goes here int i=arr.length; while (i>=0){ System.out.println(arr [i-1]); i--; } } } This is the code of the project of the array lesson in java(reversing an array) .It shows the result in the java playground but it throws an erro in the project. https://code.sololearn.com/cIq8UnFF3281/?ref=app

2nd Dec 2021, 11:20 AM
KADHM
KADHM - avatar
3 Answers
+ 1
in for cycle it has to be i>0 so it won't try to print out the char element with the index it does not exist in
2nd Dec 2021, 11:25 AM
cadbrooke
cadbrooke - avatar
0
Scoozak ,ohh thanks,I try that
2nd Dec 2021, 11:27 AM
KADHM
KADHM - avatar
0
Scoozak,thanks .It worked
2nd Dec 2021, 11:30 AM
KADHM
KADHM - avatar