2016-01-15 87 views
-2

我有這段代碼找到下面的迴文;我需要能夠從用戶輸入字符串中刪除所有數字,空格和標點符號,所以我一直在使用replaceAll。當我的代碼中只有String input = str.toLowerCase();String newInput = input.replaceAll("[0-9]+", "");時,沒有問題。它刪除了數字並繼續。但是,當我嘗試添加標點或空格時,出現StringIndexOutOfBoundsException。什麼導致這個StringIndexOutofBounds異常?

例:I輸入Anna.55

所有replaceAll聲明的下一行,System.out.println(newestInput);,將打印出anna但到達while循環時,立即引發錯誤,並指出,問題是指數6.

從我的理解(我仍然在學習Java和我不熟悉replaceAll)與replaceAll("\\s", "")刪除的空間將消除由以前replaceAll語句留下的空隙,因此就沒有指數6(甚至4)。在索引6不存在時,如何發生錯誤?

import java.util.Scanner; 

public class PalindromeTester { 
    public static void main (String[] args) { 
     String str; 
     String another = "y"; 
     int left; 
     int right; 
     Scanner scan = new Scanner (System.in); 
     while (another.equalsIgnoreCase("y")) { 
      System.out.println("Enter a potential palindrome:"); 
      str = scan.nextLine(); 
      left = 0; 
      right = str.length() - 1;   
      String input = str.toLowerCase(); 
      String newInput = input.replaceAll("[0-9]+", ""); 
      String newerInput = input.replaceAll("\\W", ""); 
      String newestInput = newerInput.replaceAll("\\s", "");   
      System.out.println(newestInput); 
      while (newestInput.charAt(left) == newestInput.charAt(right) && left < right) { 
       left++; 
       right--; 
      } 
      System.out.println(); 
      if (left < right) 
       System.out.println("That string is not a palindrome."); 
      else 
       System.out.println("That string is a palindrome."); 
      System.out.println(); 
      System.out.print ("Test another palindrome (y/n)? "); 
      another = scan.nextLine(); 
     } 
    } 
} 
+0

首先''input.replaceAll(「\\ W」,「」)''不應該在這裏使用'newInput'嗎?第二:是否認爲計算'right'是一個好主意,_before_你減少你的源字符串的大小? – Tom

+0

'如果在索引爲6時出現錯誤,它不再存在嗎?'這不是回答你的問題嗎?您需要包含堆棧跟蹤的相關部分,以便人們更好地幫助您。 – John3136

回答

2

您使用right = str.length() - 1;確定輸入的長度,但你改變什麼之後(和你比較的)輸入...

String input = str.toLowerCase(); 
String newInput = input.replaceAll("[0-9]+", ""); 
String newerInput = input.replaceAll("\\W", ""); 
String newestInput = newerInput.replaceAll("\\s", ""); 

System.out.println(newestInput); 
while (newestInput.charAt(left) == newestInput.charAt(right) && left < right) { 

這意味着String沒有不再是原來的長度,在你的榜樣,它的1字符短

相反,計算newestInput代替

的長度210
right = newestInput.length() - 1; 
System.out.println(newestInput); 
while (newestInput.charAt(left) == newestInput.charAt(right) && left < right) { 
+0

*「它縮短了1個字符」* ..這就是有趣的部分,它應該縮短3個字符:D。但解決方案很簡單,所以它不再有趣。 – Tom

+1

input ='Anna.55' output ='anna55'; OP在'String'的錯誤實例上使用'repalceAll'的事實,考慮到問題的性質,我想他們會得到1「免調試卡」) – MadProgrammer

+0

這是我的疏忽,對不起。我確信問題在於我如何使用'replaceAll'錯誤,我沒有檢查我的語句的順序/位置。謝謝! –

2

兩兩件事第一:

我覺得

input.replaceAll("\\W", ""); 

應該

newInput.replaceAll("\\W", ""); 

而令牌被刪除之後,應計算而不是之前,像這樣:

left = 0; 
String input = str.toLowerCase(); 
String newInput = input.replaceAll("[0-9]+", ""); 
String newerInput = newInput.replaceAll("\\W", ""); 
String newestInput = newerInput.replaceAll("\\s", ""); 
right = newestInput.length() - 1; 

否則right可能大於newestInput的長度,您將得到java.lang.StringIndexOutOfBoundsException

0

實際上,測試一個字符串是否是迴文的更簡單的方法是,如果它是相同的向前和向後。

相關問題