Hello, I am trying to get used to working with iterators, and I've lost my way once again. My code is below and I've included my compiler errors first. I know there are other issues with my code which I will clear up later (I lumped in the node class as you see yet unedited) BUT, I am mainly interested in getting this program to compile first. Thank you in advance for any helpful suggestions.
Compiler errors:
>>> javac PrincessListDemo.java
PrincessListDemo.java:8: cannot find symbol
symbol : method iterator()
location: class PrincessList
PrincessList.ListIterator itr = list.iterator();
^
.\PrincessList.java:103: cannot find symbol
symbol : constructor PrincessNode(java.lang.String,<nulltype>)
location: class PrincessList.PrincessNode
previous.link = new PrincessNode(newData, null);
^
.\PrincessList.java:105: cannot find symbol
symbol : method addToStartOfList(java.lang.String)
location: class PrincessList
PrincessList.this.addToStartOfList(newData);
^
.\PrincessList.java:108: cannot find symbol
symbol : constructor PrincessNode(java.lang.String,PrincessList.Princes sNode)
location: class PrincessList.PrincessNode
PrincessNode temp = new PrincessNode(newData, position);
^
4 errors
import java.util.NoSuchElementException; import java.io.Console; import java.util.*; public class PrincessList //here is the linked list class { private class PrincessNode //here is the self-contained (no separate file) inner node class { private String name; //suitor's name private int count; // private PrincessNode link; //Instance Variable of type PrincessNode1 that is named link public PrincessNode() { link = null; name = null; count = 0; } public PrincessNode (String newName, int newCount, PrincessNode linkValue) { setData(newName, newCount); link = linkValue; } //below here to: public void setData(String newName, int newCount) { name = newName; count = newCount; } public void setLink(PrincessNode newLink) { link = newLink; } public String getName() { return name; } public int getCount() { return count; } public PrincessNode getLink() { return link; } //to above here eliminate mutator and accessor methods } public class ListIterator //an inner class for iterators for PrincessList { private PrincessNode position; private PrincessNode previous; //previous value of position public ListIterator() { position = head; //Instance variable head of outer class previous = null; } public void restart() { position = head; //Instance variable head of outer class previous = null; } public String next() { if (!hasNext()) throw new NoSuchElementException(); String toReturn = position.name; previous = position; position = position.link; return toReturn; } public boolean hasNext() { return (position != null); } public String peek() //Returns the next value to be returned by next(); if false, throws exception { if (!hasNext()) throw new IllegalStateException(); return position.name; } public void addHere(String newData) { if(position == null && previous != null) previous.link = new PrincessNode(newData, null); else if (position == null || previous == null) PrincessList.this.addToStartOfList(newData); else { PrincessNode temp = new PrincessNode(newData, position); previous.link = temp; previous = temp; } } public void changeHere(String newData) { if (position == null) throw new IllegalStateException( ); else position.name = newData; } public void delete() { if(position == null) throw new IllegalStateException(); else if (previous == null) { head = head.link; position = head; } else { previous.link = position.link; position = position.link; } } private PrincessNode head; //this head here private to public public ListIterator iterator() { return new ListIterator(); } public void PrincessList() { head = null; } public void addToStartOfList(String suitorName, int suitorCount) //adds new node to start of the list { head = new PrincessNode(suitorName, suitorCount, head); //above parameters include head because head used to point to the old first node (now adding a new node) so if we use the name head //on the right-hand side of the assignment operator, head will denote a reference to the old first node. //adding and deleting nodes is easiest at the start of the linked list. } public boolean deleteHeadNode() //deletes the first node connecting head with the old second node. { if (head != null) { head = head.getLink(); return true; } else return false; } public int NumberOfNodesInList() { int count = 0; PrincessNode position = head; while (position != null) { count++; position = position.getLink(); } return count; } public boolean contains(String name) { return(find(name) != null); } private PrincessNode find(String target) { PrincessNode position = head; String nameAtPosition; while (position != null) { nameAtPosition = position.getName(); if(nameAtPosition.equals(target)) return position; position = position.getLink(); } return null; } public void outputPrincessList() { PrincessNode position = head; while (position != null) { System.out.println(position.getName() + " \t " + position.getCount()); position = position.getLink(); } } } } import java.io.Console; public class PrincessListDemo { public static void main(String[] args) { PrincessList list = new PrincessList(); PrincessList.ListIterator itr = list.iterator(); Console console = System.console(); System.out.print("\nHello Eve...\n "); System.out.print("\nHow many suitors are pursuing your hand?\n "); String input = console.readLine(); int numSuitors = Integer.valueOf(input); System.out.print("\nSo there are " + numSuitors + " suitors who have asked for your hand."); for (int i=1; i<=numSuitors; i++) { int j=i; System.out.print("\nPlease enter the name of suitor #" + i + "."); String SuitorName = console.readLine(); itr.addToStartOfList(SuitorName, j); } System.out.print("\nPrincess Eve's list of suitors: "); System.out.print("\n------------------------------:\n"); itr.restart(); while(itr.hasNext()) System.out.println(itr.next()); System.out.println(); //list.outputPrincessList(); } }