Frage

I have the following text:

"better better yes yes yes better finally the rock rock the".

I wrote a method which takes each word and adds it to a linked list. Then I wrote another method which use merge sort.

The linked list I get after merge sort is as follows:

"better better better finally rock rock the the yes yes yes".

So the above two methods work fine. :-)

I would like just to print the list when each node is shown only once along with a counter value which shows how much times it is shown in the list. The duplicate nodes should not be removed from the linked list.

So the result I should get is:

better 3
finally 1
rock 2 
the 2
yes 3

Code:

public class WordNode
{

private String _word;
private WordNode _next;

/**
 * Constructor for objects of class WordNode
 */

public WordNode(String wrd, WordNode node) 
{
    this._word = wrd;
    this._next = node;

}


public class TextList
{

   private WordNode _head;


  public TextList ()
  {
     _head = null;   
  }

So I wrote the following toString() method:

public String toString()
{ 
    String result = "";

    int count = 1;

    WordNode currentNode = _head;
    WordNode tmp = currentNode.getNext();

    if (currentNode == null)
        return null;    


    while(currentNode != null)
    {
        tmp = currentNode.getNext();

        while (tmp != null && currentNode.getWord().equals(tmp.getWord()))  
        {
            count++;
            tmp = tmp.getNext();
        }

        result += currentNode.getWord() + "\t" + count + "\n";
        count = 1;
        currentNode.setNext (tmp);
        currentNode = currentNode.getNext();            
    }


    return ("The List is: " + result);

}

But after printing, I get:

The List is: better 3
finally 1
rock 2
the 1
the 1
yes 3

During debug, I found that the toString() method will not count properly the last word which is being added to the linked list ('the'...).

"the" is (a) shown twice instead of just once (b) its counter is 1 instead of 2

Please assist.

#####################################

I added more information:

I have another tester class which basically just calls the constructor.

String text1 ="better better yes yes yes better finally the rock rock the";
TextList list1 = new TextList(text1);
System.out.println("\nResult: ");
System.out.println(list1);

The TextList constructor basically parse the full text, split it properly, and then calls another method which adds each text to the linked list.

  public TextList (String text)
    {   
        String textToAdd;

        int i = 0, j = 0;



        while (i < text.length()-1 )
        {
            System.out.print("\n");
            System.out.println("I = " + i);


            while ( (text.charAt(j) != ' ') && (j < text.length()-1 ) )
               {
                   char c = text.charAt(j);    
                   if (j != text.length())
                   j++;
                }
            //J Pointer points to the "backspace".    

            textToAdd = text.substring(i,j+1); //Puts the word in textToAdd.

            System.out.print("textToAdd -> " + textToAdd);
            System.out.print("\n");

            addToData(textToAdd); // adding the word to the linked list.

            j++; // Taking the pointer one step futher again so it will be pointing to the first charcater of the next word

            i = j;   
       }  

       _head = _head.mergeSort(_head); 

   }





public void addToData (String word)
{

 if (_head == null) 
    _head = new WordNode (word, null);  
 else
    {
     WordNode currentNode = _head;
     while (currentNode.getNext() != null)
        currentNode = currentNode.getNext();

    currentNode.setNext (new WordNode (word, null));

    }

After all words are added, the constructor finishes by calling merge sort.

No need to copy merge sort here.

Any suggestions?

Thanks in advance.

War es hilfreich?

Lösung

Your solution is too simple, please make it more complex :P

You can use Map to count objects in String.

public class Program {
    public static void main(String[] args) {
        String inputString = "better better yes yes yes better finally the rock rock the";
        String[] words = inputString.split("\\s");

        Map<String, Integer> map = new TreeMap<String, Integer>();
        for (String word : words) {
            Integer numberOfWords = map.get(word);

            if (numberOfWords == null) {
                numberOfWords = 0;
            }

            map.put(word, ++numberOfWords);
        }

        for (String word : map.keySet()) {
            System.out.println(word + " " + map.get(word));
        }
    }
}

Output:

better 3
finally 1
rock 2
the 2
yes 3
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top