1

現在、外部テキスト ファイルに保存されている略語で単語を短縮できるアプリケーションを作成しようとしています。単語の後に句読点が続く場合がありますが、これは短縮後も保持する必要があります。

省略形が見つからない場合、メソッドに元の単語を返すことはできません。見た目では、メソッドは最終的な return ステートメントに到達していません。その理由はわかりません。

public class WordShortener {


private Scanner fileIn;
private String shortenedWord ="";
private String s="";

    public WordShortener() throws Exception {
    // to be completed
    Scanner fileIn = new Scanner(new File("abbs.txt"));
    this.fileIn = fileIn;
    }

public String wordShortener( String WordToShorten ) {
    // to be completed
        String s = wordToShorten;
            String shortened ="";
        while ( shortenedWord.equals(WordToShorten) && fileIn.hasNextLine()  ) {
            String line = fileIn.nextLine();
            String punctuationChar="";
            String[] lines = line.split(",");
            String originalWord = lines[0];
            String shortenedWord = lines[1];
            String punctuations = "'?.";

            if ( punctuations.contains(s.substring(s.length() - 1)) ) {
                punctuationChar = s.substring(s.length() - 1);
            }

            if ( wordToShorten.contains(lines[0]) ) {
                String shortenedWord = s.replaceAll(wordToShorten, lines[1]);
                shortenedWord = shortened + punctuationChar;
            }
        }
        if ( shortenedWord == wordToShorten ) {
            return wordToShorten;
            }
fileIn.close();
return shortenedWord;
}

this is the otherfile that is used to conduct the shortenWord message on a given word(I have imported java util and java io in the file on my computer:



public class cmdShortener {


    public static void main(String[] args) throws Exception {
        WordShortener WS = new WordShortener();
        String return = WS.shortenWord("hello");
        System.out.println( "Shortened Message:" + return );
        }
}

コンマで区切られた略語ファイルの行の例 (これらは編集されません):

8,8

9,9

あなた、あなた

4

2 に答える 2

1

ループ条件を変更する必要があります。

while ( shortenedWord.equals(inWord) | fileIn.hasNextLine()  )

これに:

while ( shortenedWord.equals(inWord) && fileIn.hasNextLine()  )

実際、省略形が見つからないか、ファイルに残りの行がある限り、ループは継続します。行がなく、略語が見つからない場合、それは決して終わらない.

また、ビット演算子 ( and ) の代わりに常に論理演算子 ( and ) を使用する必要があります。前者&&は、必要でない場合、式の残りの部分を評価しないからです。||&|

編集:良い質問をするのに苦労しているようですが、それでもコンパイルできるコードを提供できません。ですから、私が持っているものであなたを助けようとします。実際、このコードは目的に対して複雑すぎます。あなたが望むのは、次のようなものです:

private static final String PUNCTUATIONS = "'?.!;";

public String shortenWord( String inWord ) {

    String originalWord = inWord; // keep track of original input

    // Step 1: get punctuation char and trim inWord to what is needed
    String punctuationChar = "";
    int lengthMinus1 = inWord.length() - 1;
    if (PUNCTUATIONS.contains(inWord.substring(lengthMinus1))) {
        punctuationChar = inWord.substring(lengthMinus1);
        inWord = inWord.substring(0, lengthMinus1);
    }

    while (fileIn.hasNextLine()) {
        // Step 2: get the parts from the line.
        String line = fileIn.nextLine();
        if (line.isEmpty()) {
            continue;
        }
        String[] parts = line.split(",");

        // Step 3: check that inWord is the left part
        if (inWord.equals(parts[0])) {
            // Step 4: return the result
            return parts[1] + punctuationChar;
        }
    }

    // Step 5: if nothing is found, return original.
    fileIn.close();
    return originalWord;
}   

私はそれが自明であることを願っています:)

于 2013-03-15T18:37:26.813 に答える
0

ShortShort はそれ自体がパッケージ内のクラスであるため、コード内で変数名として使用しないでjava.langください。これで、メソッドはこのブロックで空の文字列を返します:

if ( inWord.contains(parts[0]) ) {
            String Short = s.replaceAll(inWord, parts[1]);
            shortenedWord = Short + punctuationChar;
                                return shortenedWord;//This is returning blank
        }

sコードで値が空白になっている可能性があります.!!

編集
コメントであなたのポイントに行くと、 method には次のコードが必要だと思いますshortenWord:

public String shortenWord( String inWord ) 
{
    String punctuations = "'?.!;"; 
    if (punctuations.contains(String.valueOf(inWord.charAt(inWord.length() - 1)))
    {
        while (fileIn.hasNextLine())
        {
            String read = fileIn.nextLine();
            String parts[] = read.split(",");
            if (parts[0].equals(inWord.substring(0,inWord.length() - 1))))
            {
                return parts[1];
            }
        }

    }  
  fileIn.close();  
  return inWord;
}
于 2013-03-15T18:46:55.853 に答える