What's is the error (java) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What's is the error (java)

Good day guys, please am finding it difficult debugging my java code, am try to reverse a string converted into array(arr) using for loop. How i did it Char result = ' '; For( int i = arr.length; i >=0; i--) { char result =+ arr[i]; } System.out.println(result); Error = possible lossy conversion int to char. But am still having errors, please help 🙏 me. Thanks 😊 Sir YUGRAH i have done that but still having issues.

1st May 2021, 3:16 PM
Hillary Chikendu
Hillary Chikendu - avatar
4 Answers
+ 7
Hillary Chikendu 1 - There is no syntax like =+ It should be += 2 - There should not be result as a Char because char store one character only so there should be String. 3 - if you declare variable inside loop then you cannot access outside the scope. Here is correct solution of your problem. String name = "Anant"; char[] arr = name.toCharArray(); String result = ""; for (int i = arr.length - 1; i >= 0; i--) { result += arr[i]; } System.out.println (result);
1st May 2021, 5:30 PM
A͢J
A͢J - avatar
+ 4
Correction : String result; //it's a string not char For(int i =arr.length-1;i>=0 ;i--) //arr.length-1 because arr.length gives an exception { result +=arr[i]; // =+ is nothing }
1st May 2021, 4:49 PM
PIYUSH
+ 3
Actually you declared result inside the loop you can't access it outside
1st May 2021, 3:19 PM
YUGRAJ
+ 2
Thanks everyone the problem is solved.
1st May 2021, 7:50 PM
Hillary Chikendu
Hillary Chikendu - avatar