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:
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:
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:
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
, "Choose file to use or Quit."
, "Create Word List"
, JOptionPane.YES_NO_OPTION
, JOptionPane.PLAIN_MESSAGE
, null
, choices
, "Not used"
);
switch (response) {
case 0: case 1:
createWordList(choices[response]);
break;
case 2: case -1:
System.exit(0);
default:
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);
}
}