+ 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.
4 Respuestas
+ 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);
+ 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
}
+ 3
Actually you declared result inside the loop you can't access it outside
+ 2
Thanks everyone the problem is solved.