Sure, here’s a Java program to reverse a string without using any built-in methods:
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
String reversed = reverse(input);
System.out.println("Reversed string: " + reversed);
}
public static String reverse(String str) {
StringBuilder builder = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
builder.append(str.charAt(i));
}
return builder.toString();
}
}
In this program, we first prompt the user to enter a string using the `Scanner` class. Then, we call the `reverse()` method to reverse the string and store the result in a new string variable. The `reverse()` method uses a `StringBuilder` to build the reversed string character by character in a loop that iterates from the end of the original string to the beginning.
Finally, we print the reversed string to the console using the `println()` method.
Note that this program uses a `StringBuilder` to build the reversed string, which is more efficient than concatenating strings using the `+` operator.