2013-03-15 39 views
1

我目前正在嘗試創建一個應用程序,該應用程序可以縮短存儲在外部文本文件中的縮寫 的單詞。這個詞可以跟着標點符號 這些字符在縮短之後也應該保留。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

+1

是'inWord'是空白的,或者是其他的(第一)'返回'這是返回空字符串。 – NPE 2013-03-15 18:37:01

+0

讓我直接問..輸入'八'嗎?你想輸出爲'八'嗎?!;' – 2013-03-15 20:04:50

+0

不,如果輸入是8,輸出應該是數字8 – Rawr 2013-03-15 20:13:05

回答

1

你應改變回路條件:

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

這樣:

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

事實上,環路,只要你沒有找到一個縮寫或有文件中剩餘的行繼續。如果你沒有更多的線條,並沒有找到任何縮寫,那麼它永遠不會結束。

此外,你應該總是使用邏輯運算符(&&||),而不是位運算符(&|),因爲如果沒有必要前人不計算表達式的其餘部分。

編輯:我看到你正在努力提出一個很好的問題,但你仍然沒有給出可編譯的代碼。所以我會盡我所能幫助你。實際上,這個代碼太複雜了,無法實現。你需要的是更多像這樣:

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; 
} 

我希望這是不言自明的:)

+0

我試過這個,但是當我試圖縮短一個可以縮寫的單詞(如下所示)時,它將不返回任何C:\ Java ShortenuerUtility「hi」 縮短的消息: – Rawr 2013-03-15 18:41:41

+0

您可以將輸入文件的行與「嗨「? – 2013-03-15 18:46:48

+0

對不起,應該說不能縮寫 – Rawr 2013-03-15 18:52:33

0

首先不要在代碼中使用Short變量名短本身就是java.lang類這是附註。現在你的方法是在該塊返回空字符串:

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

這可能是因爲s越來越值爲空在你的代碼!

編輯
去在評論你的觀點,我認爲你需要的方法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; 
} 
相關問題