我目前正在尝试创建一个可以使用存储在外部文本文件中的缩写词来缩短单词的应用程序。单词后面可以跟标点符号,缩短后也应该保留。
如果找不到它的缩写,我无法让该方法返回原始单词。从表面上看,该方法没有达到我的最终退货声明,我不确定为什么。
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
九,9
你,你