2013-08-30 52 views
0

我正在嘗試創建一個處理文件導入的每個單詞的單詞類。讀取文件,並用Word類處理單詞,方法

詞類需要區分句子結尾/句首的標點符號,增加有效詞,增加每個音節。

我的問題是讓掃描儀將字傳遞到Word類的方法來處理它們。錯誤是在「word = scan.next();.」錯誤消息是「不兼容的類型:必需的Word,找到字符串」。

謝謝您的幫助...

System.out.println("You chose to open the file: " + 
      fc.getSelectedFile().getName()); 
      scan = new Scanner(fc.getSelectedFile()); 
      while (scan.hasNext()) 
      { 
       Word word = new Word(); 
       word = scan.next(); 

詞類

public class Word { 
    private int wordCount, syllableCount, sentenceCount; 
    private double index; 
    private char syllables [] = {'a', 'e', 'i', 'o', 'u', 'y'};; 
    private char punctuation [] = {'!', '?','.'};; 


    public Word() 
    { 
     wordCount = 0; 
     syllableCount = 0; 
     sentenceCount = 0; 
    } 

    public void Word(String word) 
    { 
     if(word.length() > 1) 
     { 
      for(int i=0; i < punctuation.length; i++) 
      { 
       if(punctuation[i] != word.charAt(word.length()-1)) 
       { 
        wordCount++; 
       } 
       else 
        sentenceCount++; 
      } 
      for(int i = 0; i < syllables.length; i++) 
      { 
       for(int j = 0; j < word.length(); j++) 
       { 
        if(syllables[i] == word.charAt(j)) 
        { 
         syllableCount++; 
        } 
       } 
      } 
     } 
     System.out.println(word); 
    } 
} 
+0

一個'String'不是'Word' – nachokk

回答

2

的問題是,scan.next()返回String不是Word對象,你可以不分配給它這樣的。

試試這個:

while (scan.hasNext()) { 
    Word word = new Word(scan.next()); 
    //... 
} 

要做到這一點,你需要一個這樣的構造:

public Word(String s){ 
    //... 
} 
+0

-1,因爲他還沒有構造收到'字符串' – nachokk

+0

這是一個方法簽名,而不是構造函數 – nachokk

+0

我的錯誤,我編輯了答案。 – 2013-08-30 19:31:27

相關問題