0

Why won't this work?

Code: 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 arrayLength; arrayLength = arr.length(); String result; for (int arrLength = arrayLength - 1; arrLength >= 0; arrLength--) { char transResult = arr[arrLength]; result += transResult; } System.out.println(result); } } Error Message: /usercode/Program.java:12: error: cannot find symbol arrayLength = arr.length(); ^ symbol: method length() location: variable arr of type char[] 1 error

19th Jan 2022, 7:05 PM
Eric Li
Eric Li - avatar
2 Respostas
+ 1
And you need to initialize String result. String result = ""; Btw: you don't need an extra variable for arr.length, you can just use arr.length directly. for(int arrLength = arr.length - 1; arr.length >= 0; arrLength--){ }
19th Jan 2022, 7:31 PM
Denise Roßberg
Denise Roßberg - avatar
0
length is a property, so it would be arr.length instead of arr.length().
19th Jan 2022, 7:09 PM
Coding Cat
Coding Cat - avatar