0

Why is this code not working ( java) ?

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 i = arr[].length(); for (int k = i; k>=0; k--){ System.out.print(arr[k]); } //your code goes here } }

26th Sep 2022, 2:59 AM
Devansh
3 odpowiedzi
+ 4
This line is wrong: int i = arr[].length() It should be int i = arr.length()
26th Sep 2022, 5:14 AM
Apollo-Roboto
Apollo-Roboto - avatar
+ 2
Additionally, the for...loop counter <k> should've been initialized by <i> - 1 because last element's index in array is array's length minus one. (Edit) Also please, put Java in post tags rather than a single question mark. https://code.sololearn.com/W3uiji9X28C1/?ref=app
26th Sep 2022, 5:18 AM
Ipang
+ 1
I believe that is the code that sololearn gave it to you so you can type code below the part of comments that says,"your code goes here" so my take is you should try solving that problem.at hand by keying in code below that line and that way it may go ahead and run if it gets difficult then see solution top right corner of the same interface
26th Sep 2022, 9:32 AM
Kelvin Kiprop Kemboi
Kelvin Kiprop Kemboi - avatar