First, describe
The definition of palindrome:"palindrome number" is the same integer that reads backwards. If the odd number of numbers: 98789, the number is 98789, the reading is 98789, even the number of the number 3223 is a palindrome number.
We are today to expand the number of palindrome to a combination of letters and numbers palindrome, such as Adgu6776ugda is also a palindrome, we use three ways to determine whether this type of string is a palindrome:
1. Call the reverse () method of the StringBuffer class object, after which the string is flipped and compared to the previous string, if the equality is a palindrome and vice versa;
2. Use low and high two variables corresponding to the index of the symmetric position of the string, all the characters in the symmetric position are equal to palindrome, and vice versa;
3. Use a variable i to represent the subscript of a string and find the index relationship of the symmetric position to determine if the string is a palindrome.
Second, the source code
Import Java.util.scanner;import javax.swing.joptionpane;/** * palindrome Description: Read the past and backwards read back the string sequence is the same palindrome, such as: 1H3GKG3H1 is a palindrome * @author Tong * */public class Palindromeignorenonalphanumeric {public static void main (string[] args) {//can use JAVAX.SWING.J The Showinputdialog () method in the Optionpane class prompts the user to enter a string of letters or numbers string s = Joptionpane.showinputdialog ("Please enter the input string:"); Call the Ispalindromebybuffer () method String output = S + (Ispalindromebybuffer (s)? " Is ":" not ") +" palindrome. "; Joptionpane.showmessagedialog (null, Output); s = Joptionpane.showinputdialog ("Please enter input string:"); Call the Ispalindromebycharat () method output = S + (Ispalindromebycharat (s)? " Is ":" not ") +" palindrome. "; Joptionpane.showmessagedialog (null, Output); Use the scanner (system.in) method to prompt the user for a string System.out.println ("Please enter the input string:"); Scanner in=new Scanner (system.in); String Strorigin=in.next ();//Call Ispalindromebycharatsingle () method System.out.print (Strorigin + ( Ispalindromebycharatsingle (s)? " Is ":" not ") +" palindrome. "); }/** * by callingThe reverse () method of the StringBuffer object to determine whether the string is equal before and after the rollover, and determines whether it is a palindrome * @param s * @return */public static Boolean Ispalindro Mebybuffer (string s) {string strorigin = Filterletteranddigit (s); Strorigin as an input parameter, constructs a StringBuffer object stringbuffer strbuf = new StringBuffer (strorigin); Calls the StringBuffer object's own reverse () method for string flipping, and finally calls ToString () to return a string of type strings Strafterreverse = Strbuf.reverse (). To String (); The Equals () method is used to determine if the original string and the inverted string are equal, to decide whether it is a palindrome return strorigin.equals (Strafterreverse); /** * Whether a palindrome is judged by whether the symmetric position string in the string is the same, two variables low and high are used to index the symmetric position of the string, respectively @param s * @return */PU Blic Static Boolean Ispalindromebycharat (string s) {//low and high corresponds to the index of the symmetric position of the string, in order to determine if all the symmetric position characters are the same int low = 0; int high = S.length ()-1; while (Low < high) {if (S.charat (low)! = S.charat (high)) return false; Not a palindrome low++; high--; } return TruE is a palindrome}/** * Whether the string is the same as the symmetric position string to determine if it is a palindrome, here is a variable i to correspond to the string symmetric position index * @param s * @return */P Ublic Static Boolean ispalindromebycharatsingle (String s) {//Use a variable with a symmetric subscript to determine if all the symmetric position characters are the same for (int i=0;i<s.len Gth ()/2;i++) {/////Only the current half of the string and the last half of the string correspond to the same position, then the palindrome, only a character with a symmetric position is not a palindrome if (S.charat (i)!=s.charat (s.length ()-i-1)) {Retu RN false; }} return true; /** * Filter letters or numbers by calling Character.isletterordigit (char char) to determine if the combination of letters or numbers is a palindrome * @param s * @return String */public static String Filterletteranddigit (string s) {StringBuffer strbuf = new StringBuffer (); for (int i = 0; i < s.length (); i++) {if (Character.isletterordigit (S.charat (i))) {//Call Stringbu Ffer Append (char char) method, the input letters or numbers are added to Strbuf.append (S.charat (i)); }}//Returns the string type, needs to convert StringBuffer to string, calls the ToString () method of the StringBuffer object to return strbuf.tOstring (); } }
Java uses 3 ways to determine whether a user-entered string is a palindrome