Where is error occured in this code? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Where is error occured in this 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(); String rev=arr.length; for(int i =rev-1;i>=0;i--) { rev+=arr.charAt(str(i)); System.Out.print(rev); } //your code goes here } }

6th Sep 2021, 4:18 AM
Amala Yakin
4 Respostas
+ 2
There are multiple issues with your code. Probably because you seem to be mixing different strategies on reversing a string. If you want to keep the input string split into an array of characters; 1. String rev = arr.length; These are incompatible types. You're attempting to set an integer to a String variable. Instead use; String rev = ""; 2. rev-1 in your for loop. Here try; arr.length-1 instead 3. In your loop you are attempting to get the charAt of an array. charAt() works with a String type. Just access the array by the index of i arr[i] 4. 'Out' should not be capitalized and the output should occur outside of and after the loop. for (...; ...; ...) { ... } System.out.println(rev); If you want to skip making the string into an array and just loop over it directly; 1. See step 1, 2, 4 above! 2. Remove the line that converts text to a char array. 3. Change arr.charAt(str(1)) to; text.charAt(i);
6th Sep 2021, 6:12 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Not quite, but it's closer. Change; rev+=text(i); to rev += arr[i]; And move System.out.println(rev); So it is after the loops closing curly brace }.
6th Sep 2021, 6:44 AM
ChaoticDawg
ChaoticDawg - avatar
0
String rev = ""; // not length here int length = arr.length; // not String here for (int i=length-1; i>=0; i--) { // not rev //rev += arr.charAt( str(i) ); // charAt() is String method not usable for array rev += arr[i]; // System.out.print(rev); // not here } System.out.print(rev); // not uppercase Out
6th Sep 2021, 6:13 AM
zemiak
0
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(); String rev=""; for(int i =arr.length-1;i>=0;i--) { rev+=text(i); System.out.println(rev); } //your code goes here } } // this is correct or not
6th Sep 2021, 6:33 AM
Amala Yakin