現在、外部テキスト ファイルに保存されている略語で単語を短縮できるアプリケーションを作成しようとしています。単語の後に句読点が続く場合がありますが、これは短縮後も保持する必要があります。
省略形が見つからない場合、メソッドに元の単語を返すことはできません。見た目では、メソッドは最終的な 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
あなた、あなた