2016-03-05 75 views
-1

我有一個程序,我有一個輸入的句子。該句子使用regEx/split方法分解成單詞。單詞存儲在名爲wordsInLine的數組中,我的工作是將這些單詞添加到名爲WordList的ArrayList中。使用哈希/列表輸出每個單詞句子

如果我們找到WordList和wordsInLine之間的匹配,我們繼續閱讀下一個單詞等等,等等。我的工作是輸出每個單詞。

預期輸入=遊戲的得分是五到五。

預期輸出:

一句話:

字:分數

字:中

一句話:

於是就等

主類:

package proj1; 

import java.util.ArrayList; 

public class Proj1 { 

    public static void main(String[] args) { 

    int lineWord = 0, listWord = 0; 

    String inputLine = "The score of the game is five to five"; 
    String regEx = "(, *)|(: *)|\\s"; 
    String[] wordsInLine; 

    ArrayList<Word> wordList = new ArrayList<Word>(); 

    wordsInLine = inputLine.split(regEx); 

    if (wordList.isEmpty()) 
     System.out.println("Empty List"); 



    } 
} 

詞類:

package proj1; 

class Word { 

    String Word; 
    int timesWordIsRepeated; 

    public Word (String words, int count) { 
    words = Word;  
    } 

    public String getWord() { 
    return Word; 
    } 

    @Override 
    public String toString() { 
    String theString = String.format("Word :",Word); 
    return theString; 
    } 
} 
+0

這將是有用的,如果你能確切地解釋(並舉例說明)您對特定輸入的期望輸出。 (您已經解釋了您計劃解決問題的方法,但您試圖解決的問題是什麼?也許有更好的方法來解決問題。) –

+0

對不起。我在尋找的輸出僅僅是這樣的: 一句話: 字:分數 字:中 一句話: 字:遊戲 字:是 字:五 字:要 字:五 – Mike

+0

我我很困惑這意味着什麼。我正在尋找的是具體的例子:投入和預期產出。你可以修改原來的問題來添加這些例子。 –

回答

1

可以遍歷這樣獲得的每個字:

String inputLine = "The score of the game is five to five"; 
for (String word : inputLine.split(" ")) { 
    System.out.println("Word: " + word); 
} 

打印:

Word: The 
Word: score 
Word: of 
Word: the 
Word: game 
Word: is 
Word: five 
Word: to 
Word: five 
+0

這很有效,在我看來,這是最簡單的解決方案。然而,我必須使用getter方法並將數組與ArrayList進行比較,並且如果該數組中的單詞在Array和ArrayList中均匹配並且被輸出。 雖然我很欣賞這個幫助:) – Mike

+0

這是什麼意思?比較數組和列表有什麼意義?你不是把所有的單詞都複製到列表中嗎?請澄清列表的目的是什麼。 – Atuos

0

HashMap可以更好地幫助您計算字那麼ArrayList

public static void main(String[] a) throws Exception { 

    String inputLine = "The score of the game is five to five"; 
    String regEx = "(, *)|(: *)|(\\? *)|(; *)|(! *)|(*\\(*)|(\\) *)|\\s"; 
    String[] wordsInLine; 

    Map<String, Integer> wordMap = new HashMap<String, Integer>(); 

    wordsInLine = inputLine.split(regEx); 

    for (String lineWord : wordsInLine) { 
     Integer count = wordMap.get(lineWord); 
     if (count == null) { 
      wordMap.put(lineWord, 1); 
     } else { 
      wordMap.put(lineWord, (count + 1)); 
     } 
    } 

    if (wordMap.isEmpty()) { 
     System.out.println("No entry found"); 
    } else { 
     for (Entry<String, Integer> entry : wordMap.entrySet()) { 
      // you can create Word pojo here if you want new Word(entry.getKey(), entry.getValue()) and add to some list 
      System.out.println(entry.getKey() + " : " + entry.getValue()); 
     } 
    } 

} 

UPDATE:

隨着ArrayList

public static void main(String[] a) throws Exception { 

     String inputLine = "The score of the game is five to five"; 
     String regEx = "(, *)|(: *)|(\\? *)|(; *)|(! *)|(*\\(*)|(\\) *)|\\s"; 
     String[] wordsInLine; 

     ArrayList<Word> wordList = new ArrayList<Word>(); 

     wordsInLine = inputLine.split(regEx); 

     for (String lineWord : wordsInLine) { 
      boolean wordFound = false; 
      for (Word word : wordList) { 
       if (word.getWord().equals(lineWord)) { 
        wordFound = true; 
        word.setCount(word.getCount() + 1); 
       } 
      } 
      if (!wordFound) { 
       wordList.add(new Word(lineWord, 1)); 
      } 
     } 
     if (wordList.isEmpty()) { 
      System.out.println("Empty List"); 
     } else { 
      for (Word word : wordList) { 
       System.out.println(word); 
      } 
     } 

    } 

    static class Word { 

     String word; 
     int count; 

     public Word(String word, int count) { 
      this.word = word; 
      this.count = count; 
     } 

     public String getWord() { 
      return word; 
     } 

     public void setWord(String word) { 
      this.word = word; 
     } 

     public int getCount() { 
      return count; 
     } 

     public void setCount(int count) { 
      this.count = count; 
     } 

     @Override 
     public String toString() { 
      return "Word [word=" + word + ", count=" + count + "]"; 
     } 

    } 
+0

正是我在找的東西。有沒有辦法使用ArrayList來做到這一點?只是好奇。 – Mike

+0

非常感謝!我很感激。 – Mike

0

這是我會怎麼解決你的問題:

Map<String, Integer> wordCounts = new LinkedHashMap<>(); 

String inputLine = "The score of the game is five to five"; 
for (String word : inputLine.split(" ")) { 
    int count = 1; 
    if (wordCounts.containsKey(word)) { 
     count += wordCounts.get(word); 
    } 
    wordCounts.put(word, count); 
} 

for (Map.Entry<String, Integer> entry: wordCounts.entrySet()) { 
    System.out.println("Word: " + entry.getKey() + " (" + entry.getValue() + ")"); 
} 

打印:

Word: The (1) 
Word: score (1) 
Word: of (1) 
Word: the (1) 
Word: game (1) 
Word: is (1) 
Word: five (2) 
Word: to (1) 

要使用ArrayList你可以這樣做:

List<Word> words = new ArrayList<Word>(); 

String inputLine = "The score of the game is five to five"; 

outerLoop: 
for (String s : inputLine.split(" ")) { 
    for (Word existingWord : words) { 
     if (existingWord.value.equals(s)) { 
      existingWord.count++; 
      continue outerLoop; 
     } 
    } 
    words.add(new Word(s)); 
} 

for (Word word : words) { 
    System.out.println("Word: " + word.value + " (" + word.count + ")"); 
} 

有了這個類太:

class Word { 
    final String value; 
    int count = 1; 

    public Word(String value) { 
     this.value = value; 
    } 
} 

產地:

Word: The (1) 
Word: score (1) 
Word: of (1) 
Word: the (1) 
Word: game (1) 
Word: is (1) 
Word: five (2) 
Word: to (1) 
+0

太棒了,那就是我一直在尋找的東西。有沒有辦法用ArrayList來做到這一點,或者你只能用Map/Hash來做到這一點? 無論如何,我只是好奇。 – Mike

+0

我很感激幫助。不幸的是,我認爲我應該每次遍歷列表,因爲它是一個ArrayList項目,但是這非常簡單。 – Mike

+0

我已經添加了一個ArrayList版本,但與Map相比效率較低。 –