package hanson_h_2;
//hailey hanson
//Programming Assignment 2
//April 30th, 2014
//
//This program will read in from a text file, separate the words and get rid of punctuation and lowercase letters (making them all caps)
//then it will sort the words into alphabetical order and print them in a list that includes the numbered position
//of the word so that instead of listing a word twice, it will be listed as word: #occurance, #occurance, etc.
//
public class hanson_h_2{
public static void main(String[] args) {
wordIndex wi = new wordIndex("C:\\Users\\vicki\\Desktop\\java_text_file.txt");
}
}
/********************************************************************/
package hanson_h_2;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class wordIndex {
boolean statusVar;
wList myList = new wList();
int counter=0; //will keep track of word numbers
public wordIndex(String filename) {
Scanner s = null;
String word = null;
try {
s = new Scanner(new File(filename));
while(s.hasNext()){
word = s.next();
word = word.toUpperCase();
word = word.replaceAll("[^a-zA-Z]", "");
counter++;
myList.add(word, counter);
statusVar = true;
}
} catch (FileNotFoundException e) {
statusVar = false;
}
print();
}
boolean status(){
if (statusVar == true)
return true;
else
return false;
}
void print(){
// prints the sorted list of words - one per line - followed by a space-separated
// list of references (in increasing order). The reference is the word #,
// beginning with 1 for the first word, 2 for the second, etc.
if(status() == true)
myList.print();
else if (status() == false)
System.out.println("Sorry, couldn't open file");
}
}
/********************************************************************************/
package hanson_h_2;
public class wList {
wNode header; //this is the sentinel node
public wList() {//not sure about these parameters
header = new wNode(null, null);//empty list (header points to zero and is zero and stuff)
}//end of wList constructor
void add(String data, int index){ //will add new wNode onto wList in alphabetical order
wNode newNode = null;
if (header.next == null){ //if list is empty
newNode = new wNode(data, header.next);
header.next = newNode;
newNode.list = new rList();
newNode.list.add(index);
}//end of if statement
else{ //there is already at least one thing in the list
wNode traverseNode = header.next; //will step through list to check for comparisons between these 2 nodes
while (traverseNode != null){//look through wList
if (data.equals(traverseNode.data)){ //if two wNodes have the same data
traverseNode.list.add(index); //add the index of the word onto the rList
return; //get outta town... add method town
}//end of if statement
traverseNode = traverseNode.next; //move to next node in wList
}//end of while loop
traverseNode = header.next; //if the node is not already in wList, move back to beginning of wList
wNode previousNode = header;
while (traverseNode != null){
if (data.compareTo(traverseNode.data) < 0){
newNode = new wNode(data, traverseNode); //make new node and put it in before right node
previousNode.next = newNode; //change previous pointer
break;
}//end of if statement
if (traverseNode.next == null){ //if this is the last thing in the list
newNode = new wNode(data, traverseNode.next); //make your node with the data piece, put it at end
traverseNode.next = newNode; //switch previous node's pointer to point to new node
break;
}//end of if statement
previousNode = traverseNode;
traverseNode = traverseNode.next; //move along through wList
}//end of while loop
newNode.list = new rList();
newNode.list.add(index);//add index number to the rList
}//end of else
}//end of add method
public void print(){
wNode currentNode = header.next; //start at beginning on wList
while (currentNode != null){
System.out.print(currentNode.data);//print the word
currentNode.list.print(); //print its position
System.out.println("\n"); //print a newline
currentNode = currentNode.next; //go to next node
}//end of while
}//end of print()
public class wNode
{
String data; //the data will be a word from my index
wNode next; //pointer to next node
rList list; //header node to rList. Is number associated with word
public wNode(String data, wNode next){
this.data = data;
this.next = next;
}//end of wNode constructor
}//end of wNode class
}//end of wList class
/********************************************************************************/
package hanson_h_2;
public class rList {
rNode header;
public rList() {
header = new rNode(0, null);
}
void add(int ref){ //adds word to list and ref to the end of the list
rNode currentNode = header.nextNode;
if (header.nextNode == null){
rNode newNode = new rNode(ref, header.nextNode);
header.nextNode = newNode;
}
else {
while(currentNode.nextNode != null) //while i am not at the end of the list
currentNode = currentNode.nextNode; //step forward one node
rNode newNode = new rNode(ref, currentNode.nextNode);
currentNode.nextNode = newNode;
}
}
public void print(){
rNode currentNode = header.nextNode;
while (currentNode != null){
System.out.print(" " + currentNode.index);
currentNode = currentNode.nextNode;
}
}
private class rNode {
int index; //data piece of node to tell where word is located in file
rNode nextNode; //pointer to next node
//should maybe set nextNode equal to null...?
public rNode(int ref, rNode next) {
index = ref;
nextNode = next;
}
}
}
No comments:
Post a Comment