我目前正在嘗試創建一個應用程序,該應用程序可以縮短存儲在外部文本文件中的縮寫 的單詞。這個詞可以跟着標點符號 這些字符在縮短之後也應該保留。hibby jibby 123456789
如果找不到 的縮寫,我無法讓該方法返回原始單詞。通過它的外觀方法是沒有達到我的最終返回聲明和 我不確定爲什麼。
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
你,U
是'inWord'是空白的,或者是其他的(第一)'返回'這是返回空字符串。 – NPE 2013-03-15 18:37:01
讓我直接問..輸入'八'嗎?你想輸出爲'八'嗎?!;' – 2013-03-15 20:04:50
不,如果輸入是8,輸出應該是數字8 – Rawr 2013-03-15 20:13:05