2012-09-11 65 views
4

这应该是非常简单的(我认为),但我只是不能正确地做到......:|如何在JAVA中检查字符串数组中的相等字

任务如下:

询问用户一些输入。输入必须分成单个单词并放入一个数组中。所有单词都应该被计算在内如果存在相同的单词,则它们会在输出中获得“+1”。 最后,我想打印出来,并希望列表中正确数量的单词。我得到了前两列的权利,但相同的单词柜台让我很头疼。如果发现一个单词相同,它不会在生成的列表中出现两次! !

我是一个完整的JAVA新手,所以请在代码判断上做些什么。 ;)

这里是我到目前为止的代码:

package MyProjects; 

import javax.swing.JOptionPane; 

public class MyWordCount { 
public static void main(String[] args) { 

    //User input dialog 
    String inPut = JOptionPane.showInputDialog("Write som text here"); 

    //Puts it into an array, and split it with " ". 
    String[] wordList = inPut.split(" "); 

    //Print to screen 
    System.out.println("Place:\tWord:\tCount: "); 

    //Check & init wordCount 
    int wordCount = 0; 

    for (int i = 0; i < wordList.length; i++) { 

     for (int j = 0; j < wordList.length; j++){ 

      //some code here to compare 
      //something.compareTo(wordList) ? 

     } 

     System.out.println(i + "\t" + wordList[i]+ "\t" + wordCount[?]); 
    } 

} 
} 
+0

也许我应该改变两个for-loops用for-each循环insted? – CustomCase

+1

使用'列表 words = Arrays.asList(inPut.split(“”));'然后使用'Collections.frequency(words,word)'来确定单词的实例数量。 – oldrinb

+0

这还没有得到您的满意吗?你需要更多信息吗? – Link19

回答

0

感谢试图帮助我。 - 这是我最终做的:

import java.util.ArrayList; 

import javax.swing.JOptionPane; 

public class MyWordCount { 
public static void main(String[] args) { 

    // Text in 
    String inText = JOptionPane.showInputDialog("Write some text here"); 

    // Puts it into an array, and splits 
    String[] wordlist = inText.split(" "); 

    // Text out (Header) 
    System.out.println("Place:\tWord:\tNo. of Words: "); 

    // declare Arraylist for words 
    ArrayList<String> wordEncounter = new ArrayList<String>(); 
    ArrayList<Integer> numberEncounter = new ArrayList<Integer>(); 

    // Checks number of encounters of words 
    for (int i = 0; i < wordlist.length; i++) { 
     String word = wordlist[i]; 

     // Make everything lowercase just for ease... 
     word = word.toLowerCase(); 

     if (wordEncounter.contains(word)) { 
      // Checks word encounter - return index of word 
      int position = wordEncounter.indexOf(word); 
      Integer number = numberEncounter.get(position); 
      int number_int = number.intValue(); 
      number_int++; 
      number = new Integer(number_int); 
      numberEncounter.set(position, number); 

      // Number of encounters - add 1; 
     } else { 
      wordEncounter.add(word); 
      numberEncounter.add(new Integer(1)); 
     } 

    } 

    // Text out (the list of words) 
    for (int i = 0; i < wordEncounter.size(); i++) { 
     System.out.println(i + "\t" + wordEncounter.get(i) + "\t" 
       + numberEncounter.get(i)); 
    } 

    } 
} 
2

因此,为了比较两个字符串,你这样做:

String stringOne = "Hello"; 
String stringTwo = "World"; 
stringOne.compareTo(stringTwo); 
//Or you can do 
stringTwo.compareTo(stringOne); 

不能将字符串比较String数组一样在你的评论。你将不得不在这个字符串数组中的一个元素,并比较(所以stringArray [elementNumber])。

要计算有多少单词,如果要确定重复单词的数量,则需要有一个整数数组(因此请创建一个新的int [])。新的int []中的每个位置都应该与您的单词数组中的单词相对应。这将允许您计算单词重复的次数。

6

你可以使用Hashmap来做到这一点。一个Hashmap存储键值对,每个键必须是唯一的。

因此,在你的情况下,关键是你有分裂和值将是它的数字符串的话。

一旦你输入分成单词并把它们放到一个字符串数组,把第一个字,作为重点,到HashMap和1,因为它的价值。对于每个后续单词,可以使用函数containsKey()将该单词与Hashmap中的任何现有密钥进行匹配。如果它返回true,则将该键的值(count)增加1,否则将该词和1作为新的键值对添加到Hashmap中。

1
import java.util.ArrayList; 
import java.util.regex.PatternSyntaxException; 

import javax.swing.JOptionPane; 

public class Main { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 

    //Print to screen 
    System.out.println("Place:\tWord:\tCount: "); 

    //User input dialog 
    String inPut = JOptionPane.showInputDialog("Write som text here"); 

    //Puts it into an array, and split it with " ". 
    String[] wordList; 
    try{ 
     wordList = inPut.split(" "); 
    }catch(PatternSyntaxException e) { 
     // catch the buggy! 
     System.out.println("Ooops.. "+e.getMessage()); 
     return; 
    }catch(NullPointerException n) { 
     System.out.println("cancelled! exitting.."); 
     return; 
    } 

    ArrayList<String> allWords = new ArrayList<String>(); 
    for(String word : wordList) { 
     allWords.add(word); 
    } 

    // reset unique words counter 
    int uniqueWordCount = 0; 

    // Remove all of the words 
    while(allWords.size() > 0) { 
     // reset the word counter 
     int count = 0; 

     // get the next word 
     String activeWord = allWords.get(0); 

     // Remove all instances of this word 
     while(doesContainThisWord(allWords, activeWord)) { 
      allWords.remove(activeWord); 
      count++; 
     } 

     // increase the unique word count; 
     uniqueWordCount++; 

     // print result. 
     System.out.println(uniqueWordCount + "\t" + activeWord + "\t" + count); 

    } 

} 

/** 
* This function returns true if the parameters are not null and the array contains an equal string to newWord. 
*/ 
public static boolean doesContainThisWord(ArrayList<String> wordList, String newWord) { 
    // Just checking... 
    if (wordList == null || newWord == null) { 
     return false; 
    } 

    // Loop through the list of words 
    for (String oldWord : wordList) { 
     if (oldWord.equals(newWord)) { 
      // gotcha! 
      return true; 
     } 
    } 
    return false; 
} 

} 
1

下面是一个使用WordInfo对象映射的解决方案,该对象记录了文本在文本中的位置并将其用作计数。 LinkedHashMap从第一次输入时就保留了按键的顺序,因此只需遍历按键即可“按照外观顺序进行投射”

您可以在保存第一个外观的情况下通过全部保存键作为小写,但将原始大小写存储在WordInfo对象中。或者只是将所有单词转换为小写,然后保留。 您可能还需要考虑拆分之前,从第一个文本中删除所有,/./"等,但你永远不会得到完美的反正。

import java.util.LinkedHashMap; 
import java.util.Map; 

import javax.swing.JOptionPane; 

public class MyWordCount { 
    public static void main(String[] args) { 

     //User input dialog 
     String inPut = JOptionPane.showInputDialog("Write som text here"); 

     Map<String,WordInfo> wordMap = new LinkedHashMap<String,WordInfo>(); 

     //Puts it into an array, and split it with " ". 
     String[] wordList = inPut.split(" "); 

     for (int i = 0; i < wordList.length; i++) { 
      String word = wordList[i]; 
      WordInfo wi = wordMap.get(word); 
      if (wi == null) { 
       wi = new WordInfo();    
      } 
      wi.addPlace(i+1); 
      wordMap.put(word,wi);   
     } 

     //Print to screen 

     System.out.println("Place:\tWord:\tCount: "); 

     for (String word : wordMap.keySet()) {   

      WordInfo wi = wordMap.get(word);   
      System.out.println(wi.places() + "\t" + word + "\t" + wi.count()); 
     } 

     } 
} 

而且WORDINFO类:

import java.util.ArrayList; 
import java.util.List; 

public class WordInfo { 

    private List<Integer> places; 

    public WordInfo() { 
     this.places = new ArrayList<>(); 
    } 

    public void addPlace(int place) { 
     this.places.add(place); 
    } 


    public int count() { 
     return this.places.size(); 
    } 

    public String places() { 
     if (places.size() == 0) 
      return ""; 

     String result = ""; 
     for (Integer place : this.places) { 
      result += ", " + place; 
     } 
     result = result.substring(2, result.length()); 
     return result; 
    } 
} 
相关问题