I am trying to write a program that checks if a string is a palindrome and so far I know I am on the right path but when I enter my code it keeps on running for ever. I don't know what the problem is and would like help finding out the solution. In my program I want the user to enter word or words in the method Printpalindrome and then the program should know if the string is a palindrome or not.
Here is my code:
Scanner console = new Scanner (System.in);
String n = console.next();
Printpalindrome(console,n );
}
public static void Printpalindrome(Scanner console, String n){
Scanner in = new Scanner(System.in);
String orginal, reverse = "";
n = in.nextLine();
int length = n.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + n.charAt(i);
if (n.equals(reverse))
System.out.println("Entered string is a palindrome.");
}}
Aucun commentaire:
Enregistrer un commentaire