2013-04-04 23 views
0

我正在嘗試在java中使用一種方法創建一個Palindrome測試器。這是迄今爲止我所擁有的。它如此接近,我無法弄清楚爲什麼它不會說它是迴文並反轉它。迴文編碼

System.out.println("Fun with Palindromes!!"); 

    Scanner in = new Scanner(System.in); 
    System.out.println("Enter the potential palindrome (or enter exit to quit): "); 
    String x = in.nextLine(); 

    while(!x.equals("exit")) 
    { 
     String t = x.toLowerCase(); 
     String u = CleanUpString(t); 
     Boolean wordCheck = checkPalindrome(u); 
     int wordCount = x.length(); 

     String rev = ""; 

     for(int i = 0; i <x.length(); i++) 
     { 
      rev = x.charAt(i)+rev; 
     } 
     if(wordCheck == true) 
     { 
      System.out.println("The orginal string\"" + u + "\" contains" + wordCount + "characters."); 
      System.out.println("The converted string\"" + rev + "\"is a palindrome"); 
     } 
     else if(wordCheck == false) 
     { 
      System.out.println("The string \"" + u + "\" contains " + wordCount + " characters"); 
      System.out.println("\"" + rev + "\" is not a palindrome"); 
     } 
    System.out.println("\nEnter the potential palindrome, or enter exit to quit: "); 
    x = in.nextLine(); 

    } 
} 
    public static String CleanUpString(String words) 
    { 
     words = words.replace(".",""); 
     words = words.replace("," ,""); 
     words = words.replace(":",""); 
     words = words.replace("!",""); 
     return words; 


    } 



    public static boolean checkPalindrome(String baseball) 
    { 
     String rev = ""; 
     for(int i = 0; i<baseball.length()-1; i++) 
      { 
       rev = baseball.charAt(i) + rev; 

      } 
     if(rev.equals(baseball)) 
      return true; 
     else 
      return false; 

    } 

}

回答

1

這裏是我用來判斷一個字符串是否是迴文字符串或不代碼:

private static boolean checkPalindrome(String str){ 
    if (str == null) 
     return false; 
    int len = str.length(); 
    for (int i=0;i<len/2 ; i++){ 
     if (str.charAt(i) != str.charAt(len - i - 1)){ 
      return false; 
      } 
    }  
    return true; 
} 

逆轉字符串,你可以簡單地使用:

String reverse = new StringBuffer(string).reverse().toString(); 

希望這些可以幫助你。

1

import org.apache.commons.lang.StringUtils; 

String isPalindrome(String word) { 
    return StringUtils.reverse(word).equals(word); 
} 
+0

是的,這是在生產代碼中正確的做法。但是,如果這是某種編碼練習(我們面對它,看起來可能:)),底層算法也很有用:http://commons.apache.org/proper/commons-lang/javadocs/api -2.6/src-html/org/apache/commons/lang/text/StrBuilder.html#line.1870 – Palpatim

+0

確實。您應該將其作爲答案發布 – scrblnrd3

0

在這裏使用StringUtils是另一種選擇

public class PalindromeTester { 
    public static void main(String[] args) { 

try { 

    String s = args[0]; 
    int i = args[0].length()-1; 
    int i2 = args[0].length(); 
    char [] chrs = new char[i2]; 

    for (int i3 = i; i3 > -1; i3--) { 
     chrs[i2-i3-1] = (s.charAt(i3)); 
     } 

     String s2 = String.valueOf(chrs); 

     if (s2.equals(s)) { 
      System.out.println(s + " is a palindrome!"); 
     } else { 
      System.out.println(s + " is not a palindrome"); 
     } 

    } catch (ArrayIndexOutOfBoundsException e) { 
     System.out.println("Please enter at least one letter or digit!"); 
    } 

} 
    } 
0

我是這樣做的:

public class palindromeTWO 
{ 
    public static void main(String[] args) 
    { 
     Scanner scan = new Scanner(System.in); 
     int right = 0; 
     int left = 1; 

     System.out.println("Please enter a word: "); 
     String word = scan.next(); 

     int word_length = word.length(); 

     while(word.charAt(right) == word.charAt(word_length - left) && left < (word_length/2)) 
     { 
      left++; 
      right++; 
     } 

     if(word.charAt(right) == word.charAt(word_length - left)) 
     { 
      System.out.println("'" + word + "'" + " is a palindrome!"); 
     } 
     else 
      { 
       System.out.println("'" + word + "'" + " is NOT a palindrome."); 
      } 
    } 
} 
0

1日實施使用遞歸 -

import java.util.ArrayList; 
import java.util.stream.Collectors; 

public class PalindromeManager { 
private static String str = "ehcache"; 
private static ArrayList<String> list = new ArrayList<>(); 

public static void main(String[] args) { 
    test(str); 

    String output = list.stream().collect(Collectors.joining()); 

    System.out.println(output); 

    if (output.equals(str)) { 
     System.out.println("it was palindrome"); 
    } else { 
     System.out.println("Nope! it wasn't"); 
    } 
} 

private static void test(String str) { 
    if (str.length() <= 0) { 
     return; 
    } 

    String lastChar = "" + str.charAt(str.length() - 1); 
    list.add(lastChar); 
    test(str.substring(0, str.length() - 1)); 

    } 
} 

使用迭代第二實施 -

public class PalindromeManager2 { 
private static String str = "ehcache"; 

public static void main(String[] args) { 

    int startIndex = 0; 
    int lastIndex = str.length() - 1; 

    boolean result = true; 

    while (true) { 

     if (startIndex >= lastIndex) { 
      break; 
     } 

     char first = str.charAt(startIndex); 
     char last = str.charAt(lastIndex); 

     /*if (first == ' ') { 
      startIndex++; 
      continue; 
     } 

     if (last == ' ') { 
      lastIndex--; 
      continue; 
     }*/ 

     if (first != last) { 
      result = false; 
      break; 
     } 

     startIndex++; 
     lastIndex--; 

    } 

    if (result) { 
     System.out.println("Yes! It was"); 
    } else { 
     System.out.println("Nope! it wasn't"); 
    } 
    } 
} 
0

在checkPalindrome方法改變環路的狀態從i<baseball.length()-1i<baseball.length()