使用哈希/列表输出句子中的每个单词

时间:2016-03-05 08:24:16

标签: java

我有一个程序,我输入了一个句子。使用regEx / split方法将句子分成单词。这些单词存储在名为wordsInLine的数组中,我的工作是将这些单词添加到名为WordList的ArrayList中。

如果我们在WordList和wordsInLine之间找到匹配,我们将转到下一个单词,依此类推。我的工作是输出每个单词。

预期输入=游戏得分为五到五。

预期产出:

Word:

单词:分数

Word:Of

Word:

等等等等

主类:

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

3 个答案:

答案 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

答案 1 :(得分: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());
        }
    }

}

<强>更新

使用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 + "]";
        }

    }

答案 2 :(得分: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)