2012-09-11 68 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; 
    } 
} 
相關問題