main method question
I'm still reletively new to Java, so forgive if this question sounds stupid. But what I'm trying to do is open a text file, read it, create one linked list of all the tokens. then print out the list, of tokens words. I'm sure I have semantic, problems in my code but what I'm trying to do is figure out to link , the method that reads the filepublic void readInFromFile(String infilename) throws Exception to the main method, so I can test my code, and see what needs to be fixed. Here's the code below. The Node, class was graciously give to us by my proffesser so that part is all set.
The wordcount class is mine, and that's where the problem is.
publicclass Node{
publicint element;
public Node next;
/** Constructor
|
| "next" will become the successor to this new node
*/
public Node (int data, Node next )
{
element = data;
this.next = next;
}
/** return the data */
publicint getElement()
{
return element;
}
/** return the successor */
public Node getNext()
{
return next;
}
/** set the element of this node */
publicvoid setElement(int newData )
{
element = newData;
}
/** set the successor of this node */
publicvoid setNext( Node newNext )
{
next = newNext;
}
}
publicclass wordCount{
protectedstatic Node head;// head of the list
protectedlong size;// number of nodes in the list
/** Constructor -- create empty list */
public wordCount()
{
head =null;
size = 0;
}
/** addFirst -- insert a new node at the head */
publicvoid addFirst(int data )
{
Node nnode =new Node( data, head );
head = nnode;
size++;
}
publicvoid readInFromFile(String infilename)throws Exception
{
Scanner fileScan;// To scan each line of the file
try{// Attempt to open the file
fileScan =new Scanner(new File(infilename));
}catch (Exception e){// On any IO error, throw a CException
thrownew Exception("Can't read " + infilename);
}
Node p = head;
int lnum = 0;// The line number in the input file
while (fileScan.hasNext()){
while (p.next !=null)
p = p.next;
size ++;
lnum = lnum + 1;
String s = fileScan.nextLine();
StringTokenizer st =new StringTokenizer(s);
}
}
/** traverse -- print each node in order */
publicvoid traverse()
{
Node p = head;
while ( p !=null )
{
System.out.print( p.getElement() );
p = p.getNext();
if ( p !=null )
System.out.print(",");
}
System.out.println();
}
}
publicstaticvoid main ( String[] args )throws FileNotFoundException
{
wordCount myList =new wordCount();
Node p;
}
}

