2017-01-09 1 views
2

ここには以下のクラスがあります。 wordValidation()メソッドを使用してwordContentを指定すると、見つかった単語とそのインデックスのみに出力が提供されます。 "BCGAYYY"のような子音がある場合、以下のテストプログラムを使用したサンプル出力では、このケースAの間違った文字のみに出力を提供する方法(Aはコンソナートではないため) "+インデックス?しかし、これは全体の単語とそのインデックスを提供下回っ特定のインデックスへの文字列コンテンツの返却

私はチェック例外

public class InvalidWordException extends Exception { 

    public InvalidWordException (String wordContent, int theIndex) { 
     super("Wrong Word" + wordContent + theIndex); 
    } 


} 

具象クラス

public abstract class Words { 
    private String wordDetail; 
    private String wordContent; 

    public Words(String wordDetail, String wordContent) throws InvalidWordException{ 
    this.wordContent = wordContent; 
    this.wordDetail = wordDetail; 
    wordValidation(); 



    } 

    public String getWordDetail() { 
     return this.wordDetail; 
    } 

    public String getWordContent() { 
     return this.wordContent; 

    } 

    public abstract String AcceptedCharacters(); 

    public void wordValidation() throws InvalidWordException{ 

      String content = getWordContent(); 
     String theseletters = this.AcceptedCharacters(); 


     for (int i = 0; i < content.length(); i++) { 
      char c = content.charAt(i); 
      if (theseletters.indexOf(c) == -1) { 
       throw new InvalidWordException(content, i); 

      } 
     } 

    } 

    public String toString(){ 
    return getWordDetail() + getWordContent(); 

    } 

...)メソッドwordValidationを(持っている1

public class Vowels extends Words { 
    private String validVowels; 

    public Vowels(String wordDetail, String wordContent) throws InvalidWordException { 
     super(wordDetail, wordContent); 
    } 

    @Override 
    public String AcceptedCharacters() { 
     return validVowels = "AEIOU"; 
    } 

    public static void main(String[] args) { 
     try { 
      Vowels vowel = new Vowels("First Vowel Check" ,"AEIOXAEI"); 
     } catch (InvalidWordException ex) { 
      System.out.println(ex.getMessage()); 
     } 
    } 

} 

コンクリートクラス2

public class Consonants extends Words { 
    private String validConsonants; 
    public Consonants(String wordDetail, String wordContent) throws InvalidWordException{ 
     super(wordDetail, wordContent); 
    } 

    @Override 
    public String AcceptedCharacters() { 
     return validConsonants ="BCDFGHJKLMNPQRSTVXZWY"; 
    } 
    public static void main(String[] args) { 
     try { 
      Consonants consonants = new Consonants("First Consonant Check","BCGAYYY"); 
     } catch (InvalidWordException ex) { 
      System.out.println(ex.getMessage()); 
     } 

    } 
} 

テストプログラム

public static void main(String[] args) { 
     try { 
      Consonants consonants = new Consonants("First Consonant Check","BCGAYYY"); 
     } catch (InvalidWordException ex) { 
      System.out.println(ex.getMessage()); 
     } 

    } 

答えて

3

変更throw new InvalidWordException(content, i);

Javaでは

throw new InvalidWordException(content.substring(0,i), i);

に、Stringオブジェクトは不変です。元のコンテンツ文字列をそのまま渡していました。それがあなたの望む出力を与えていない理由です。

+2

ありがとうございます。 – blueGOLD

関連する問題