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 } }
4 ответов
+ 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.
}
+ 1
A͢J thanj you so much!!
0
Jay Matthews that wasn't taught in the module tho?
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...