Click here to Skip to main content
16,015,921 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Background:
A WordList object manages a linked list of WordItem objects. WordItems added to a WordList are placed in the list based on their sequence number. For example, the rhyme “Mary had a little lamb” might be added to a WordList using the set of WordItem objects 04little 02had 05lamb 01Mary 03a in which case the WordItem objects in the WordList would be ordered

01Mary 02had 03a 04little 05lamb

I'm having problem with the string that the message dialog returns when I run the code, because it just shows "WordList@55933b00" instead of the string sequence of WordItems concatenated with a space between them in order.
There are 3 classes that this project use:

WordItem Class:
Java
public class WordItem
{
  private int sequence;
  private String word;
  
  public WordItem(String seq)
  {
    sequence =  Integer.parseInt(seq.substring(0, 2));
    word = seq.substring(2);
  }
  
  public int getSequence()
  {
    return sequence;
  }
  
  public String getWord()
  {
    return word;
  }
  
  public String toString()
  {
    return sequence + word;
  }
}


WordList Class:
Java
import java.util.LinkedList;
import java.util.ListIterator;

public class WordList
{
  private LinkedList list;
  
  public WordList()
  {
    list = new LinkedList();
  }
  
  public void addWordItem(WordItem item)
  {
    ListIterator iter = list.listIterator();
    if (list == null)
      list.addLast(item);
    else
    {
      while (iter.hasNext())
      {
        if (iter.next().getSequence() > item.getSequence())
        {
          iter.previous();
          iter.add(item);
          return;
        }
      }
    }
  }
  
  public LinkedList getList()
  {
    return list;
  }
  
  public String toStirng()
  {
    String str = "";
    for (WordItem item : list)
    {
      str += item.getWord() + " ";
    }
    return str;
  }
}


And the WordListTest which is provided by the instructor & is not allowed to be modified:
Java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class WordListTest
{
 public static void main(String[] args) throws FileNotFoundException
 {
  String[] choices = {"numbers.txt", "scrambledlimerick.txt", "Quit"};
        while (true) {
            int response = JOptionPane.showOptionDialog(
                               null                       // Center in window.
                             , "Choose file to use or Quit."   // Message
                             , "Create Word List"         // Title in titlebar
                             , JOptionPane.YES_NO_OPTION  // Option type
                             , JOptionPane.PLAIN_MESSAGE  // messageType
                             , null                       // Icon (none)
                             , choices                    // Button text as above.
                             , "Not used"                 // Default button's label
                           );
            //... Use a switch statement to check which button was clicked.
            switch (response) {
                case 0: case 1:
                    createWordList(choices[response]);
                    break;
                case 2: case -1: //... Both the quit button (2) and the close box(-1) handled here.
                    System.exit(0);     // It would be better to exit loop, but...
                default: //... If we get here, something is wrong.  Defensive programming.
                    JOptionPane.showMessageDialog(null, "Unexpected response " + response);
            }
        }
 }

 private static void createWordList(String fileName) throws FileNotFoundException
 {
  WordList list = new WordList();
  Scanner in = new Scanner(new File(fileName));
  while(in.hasNext())
   list.addWordItem(new WordItem(in.next()));
  JOptionPane.showMessageDialog(null, list);
 }
}
Posted

Whatever you do there - the output just represents a list reference.
So you should call for list.toString().
 
Share this answer
 
Comments
Ytrail 26-Apr-12 6:57am    
The weird thing about it though, is I already tried that & it does not work. Why? Because the toString() method gets called by default, so calling it explicitly gives the same result (which it does because I just tested). So I'm trying to figure out why it points to a list reference because it shouldn't. It should be printing out "[]", like it did when I tested the toString() method on a regular LinkedList, but when you call it on the WordList LinkedList it prints out a reference which does not make any sense to me.
Well turns out, I'm a idiot because the problem was I misspelled the toString() method in WordList, so it was not even getting called. I had as you can see above "public String toStirng()" like an idiot, which caused me to test this program endlessly because I didn't catch it. This has taught me a valuable lesson as a programmer. Remember to do a spell checker that checks to make sure all your method names are spelled correctly. Anyway the correct solution is below:

WordList Corrected:

Java
import java.util.LinkedList;
import java.util.ListIterator;

public class WordList
{
  private LinkedList list;
  
  public WordList()
  {
    list = new LinkedList();
  }
  
  public void addWordItem(WordItem item)
  {
    if (list.size() == 0)
      list.addLast(item);
    else
    {
      ListIterator iter = list.listIterator();
      while (iter.hasNext())
      {
        if (iter.next().getSequence() > item.getSequence())
        {
          iter.previous();
          iter.add(item);
          return;
        }
      }
      iter.add(item);
    }
  }
  
  public LinkedList getList()
  {
    return list;
  }
  
  public String toString()
  {
    String str = "";
    for (WordItem item : list)
    {
      str += item.getWord() + " ";
    }
    return str;
  }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900