How to check for equal words in a string array in JAVA

It should be pretty simple (I think), but I just can't get it right ...: |

The task is as follows:

Ask the user to enter any input. The input should be divided into separate words and placed in an array. All words should be considered. If there are equal words, they get "+1" in the output. Finally, I want to print and hopefully the correct number of words counted in the list. I got the first two columns to the right, but the word counter of equal words gave me a headache. If the word is found equal, it should not appear twice in the generated list !:

I am a complete JAVA newbie, so please be kind in code.;)

Here is my code:

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[?] ); } } } 
+4
source share
5 answers

Thanks for trying to help me. -This is what I ended up with:

 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)); } } } 
0
source

You can use a hashmap for this. A hashmap stores key-value pairs, and each key must be unique.

So, in your case, the key will be the word of the string that you divided, and the value will be counted.

Once you split the input words into words and put them in a string array, put the first word as a key in the Hashmap value and 1 as the value. For each subsequent word, you can use the containsKey () function to match that word with any of the existing keys in the Hashmap. If it returns true, increase the value (quantity) of this key by one, add the word and 1 to the new key-value pair in the Hashmap.

+6
source

So, to compare the two lines, follow these steps:

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

You cannot compare a String array with a String array, as in your comment. You will need to take an element in this string array and compare it (So stringArray [elementNumber]).

To count the number of words, if you determine the number of repeated words, you would like to have an array of integers (so create a new int []). Each place in the new int [] should correspond to a word in your array of words. This will allow you to count the number of repetitions of a word.

+2
source
 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
source

It uses a solution using the WordInfo object map, which records the locations of words in the text and uses this as a counter. LinkedHashMap preserves the order of the keys from the moment they are first entered, so a simple repetition with the keys gives you a "tidy appearance"

You can make this case insensitive by preserving the first occurrence case, keeping all keys as lowercase, but keeping the original case in the WordInfo object. Or just put all the words in lower case and leave it on that. You might also consider deleting everyone , / . / " , etc. From the first text before splitting, but in any case you will never get this perfection.

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

And the WordInfo class:

 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; } } 
+1
source

Source: https://habr.com/ru/post/1433658/


All Articles