2013-12-15 117 views
0
import java.util.Scanner; 

public class WordShuffle 
{ 
    static Scanner in = new Scanner(System.in); 

    private static String getText() 
    { 
     log("in getText"); 
     String t; 
     log("Enter only alphabets"); 
     t = in.next(); 
     log("returned t= " + t); 
     return t; 
    } 

    private static void shuffleText(final String txt) 
    { 
     log("in shuffle= " + txt); 
     final int textLenght = txt.length(); 
     final char textArray[] = new char[textLenght]; 
     for (int i = 0; i < textLenght; i++) 
     { 
      textArray[i] = txt.charAt(i); 
     } 
     for (int i = 0; i < textLenght; i++) 
     { 
     } 
    } 

    public static void log(final String l) 
    { 
     System.out.println(l); 
    } 

    private static String validateText() 
    { 
     final String text = getText(); 
     log("in validate= " + text + ""); 
     final char[] tc = text.toCharArray(); 

     for (final char t : tc) 
     { 
      if (Character.isLetter(t)) 
      { 
       log("character = " + t); 
      } 
      else 
      { 
       System.out.println("Error occured, non alphabet found in text"); 
       log("error = " + t); 
       validateText(); 
      } 
     } 
     log("validate returned " + text); 
     return text; 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(final String[] args) 
    { 
     // TODO code application logic here 
     shuffleText(validateText()); 
    } 
} 

,它運行良好, 與smaple文本「ABC3」指出了錯誤3和復發的validate()方法 的請求再次輸入時,如果輸入了示例文本「abc」,它將一個接一個地返回文本「abc」和「abc3」。return語句執行兩次無環

控制檯輸出低於

run: 
in getText 
Enter only alphabets 
abc3 
returned t= abc3 
in validate= abc3 
character = a 
character = b 
character = c 
Error occured, non alphabet found in text 
error = 3 
in getText 
Enter only alphabets 
abc 
returned t= abc 
in validate= abc 
character = a 
character = b 
character = c 
validate returned abc 
validate returned abc3 
in shuffle= abc3 
BUILD SUCCESSFUL (total time: 8 seconds) 
+0

是的,的確如此。因爲'validateText()'自己調用。你的問題到底是什麼? –

回答

1

我的建議,這裏是在其他的只好回到最後正確的文本新validateText()方法

private static String validateText() { 
    String text = getText(); 
    log("in validate= " + text + ""); 
    char[] tc = text.toCharArray(); 

    for (char t : tc) { 
     if (Character.isLetter(t)) { 
      log("character = " + t); 
     } 
     else { 
      System.out.println("Error occured, non alphabet found in text"); 
      log("error = " + t); 
      log("validate returned " + text); 
      return validateText(); 
     } 
    } 
    return text; 
} 

我回到validateText()。

1

的問題是在你的else塊,你在哪裏打電話validateText()遞歸,並忽略返回值。這種情況下的方法將以任何方式返回用戶輸入的第一個值。您應該將else塊更改爲:

else { 
     System.out.println("Error occured, non alphabet found in text"); 
     log("error = " + t); 
     return validateText(); // Return the result 
    } 
0

vaidateText()被遞歸調用。因此,在第二個「正確」輸入之後,第一個「不正確」輸入將在第一個方法調用完成時返回。

+0

非常感謝你 –

1

這是因爲您沒有在出現故障時中斷執行。錯誤是在validateText遞歸中,代碼應該是:

return validateText(); 
+0

感謝您的回覆 –

0

上面的答案是正確的。同時遞歸地調用函數可能會導致溢出,所以也許你應該重新考慮你的設計。爲什麼不使用簡單的正則表達式呢? Google:正則表達式,正則表達式java 這是每個人都使用的。

+0

謝謝,將這樣做 –