简体   繁体   中英

How do you check if a string is a palindrome in java?

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 str = console.next(); 
      Printpalindrome(console, str);
    }

    public static void Printpalindrome(Scanner console, String str) {
      Scanner in = new Scanner(System.in);       
      String original, reverse = "";



      str = in.nextLine();

      int length = str.length();

      for ( int i = length - 1; i >= 0; i-- ) {
        reverse = reverse + str.charAt(i);
      }

      if (str.equals(reverse))
        System.out.println("Entered string is a palindrome.");  

      }
   }

Because of this line:

n = in.nextLine();

your program is waiting for a second input, but you already got one before entering the function.

Remove this line and it works.

Here's your program, cleaned (and tested) :

public static void main(String[] args){
    Scanner console = new Scanner (System.in);
    String n = console.next(); 
    Printpalindrome(n);
}

public static void Printpalindrome(String n){
    String reverse = "";
    for ( int i = n.length() - 1; i >= 0; i-- ) {
        reverse = reverse + n.charAt(i);
        System.out.println("re:"+reverse);
    }
    if (n.equals(reverse))
        System.out.println("Entered string is a palindrome."); 
    else
        System.out.println("Entered string is NOT a palindrome."); 
}

Of course, this isn't the best algorithm, but you already know there are many QA on SO with faster solutions (hint: don't build a string, just compare chars).

This can be implemented in a far more efficient manner:

boolean isPalindrom(String s){
    if (s == null /* || s.length == 0 ?*/) {
        return false;
    }

    int i = 0, j = s.length() - 1;
    while(i < j) {
        if(s.charAt(i++) != s.charAt(j--)) {
              return false;
        }
    }
    return true;
}

The argument for PrintPalindrom is ignored. You read another value with `in.nextLine()'. Which is the reason for your issues.

Remove

Scanner in = new Scanner(System.in);

and

n = in.nextLine();

from Printpalindrome function

and it should work.

I tried your code and what i observed was that : first of all you are making a string to enter on the line 2 of your code:

String n=console.next();

next the the program again goes to waiting when this line gets executed:

n = in.nextLine();

actually this particular line is also expecting an input so that is why the program halt at this point of time. If you enter your String to be checked for palindrome at this point of time you would get the desired result . But I would rather prefer you to delete the line

n = in.nextLine();

because, with this, you would have to enter two words which are ambiguous.

Ur code with some correction:-

import java.util.*;

class Palindrome
{
 public static void main(String args[])
 {
  String original, reverse = "";
  Scanner in = new Scanner(System.in);

  System.out.println("Enter a string to check if it is a palindrome");
  original = in.nextLine();

  int length = original.length();

  for ( int i = length - 1; i >= 0; i-- )
     reverse = reverse + original.charAt(i);

  if (original.equals(reverse))
     System.out.println("Entered string is a palindrome.");
  else
     System.out.println("Entered string is not a palindrome.");

 }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM