0

Java help, Array Reverse a string project!

I keep getting an "illegal start of type" error but I can't find what is wrong. Any help would be much appreciated!! 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 y =""; for(int x=arr.length-1; x>=0; x--){ y = y + arr[x]; } System.out.println(y); //your code goes here } }

5th Apr 2022, 12:40 AM
Peter Murphy
4 Answers
+ 4
Peter Murphy Your code is right but you have closed main method after taking input. So just remove } before the declaration of y. { // this is a block of code // you cannot access this scope variable outside of this block. //so you have declared arr inside this block so you cannot access outside, that is why you are getting error. }
5th Apr 2022, 2:05 AM
A͢J
A͢J - avatar
+ 1
A͢J thanj you so much!!
5th Apr 2022, 2:37 AM
Peter Murphy
0
Jay Matthews that wasn't taught in the module tho?
5th Apr 2022, 12:58 AM
Peter Murphy
0
StringBuilder way, if you want : String text = scanner.nextLine(); StringBuilder st = new StringBuilder(text); //create stringbulider for text System.out.println(st.reverse()); //reverse method hope it helps...
5th Apr 2022, 10:10 AM
Jayakrishna 🇮🇳