Hi again.
It would be a little messy to submit the entire code, since it consist of 9 classes and is dependent on a text file of 700+ lines (word list). The purpose of the program is to find the shortest way from the word 'fan' (satan) to the word 'gud' (god) by changing only a single letter at a time and demanding that any words in between do exist (ie, are present in a word list). After some testing I have concluded that most of the program works. It does find the word 'gud' (stored as 'wanted'). But the program fails to return the proper address (a Queue with all nodes above and it self). There is a try-statement in the makeAdress method that constantly fails since the vaule of parent is always null. Next to it, is a simple print-statement that I added to see whether the wanted word can see its parent. It cant, and causes NullPointer...
The value of parent is set in the method 'construct' and is marked ***.
I actually tried to ad a constructor that set parent directly, but it did not help me...
-----------------------------------------------------------------------------------------
public class MultiNode {
boolean isParent=false; // There are levels further down in the tree
boolean hasSon=false; // node has a nonzero number of sons.
boolean isTarget=false;
MultiNode parent;
String value;
MultiNode[] sons;
Queue adress=new Queue();
Bintree wordlist;
Bintree oldwords;
String wanted;
String[] SweAlpha={"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "å", "ä", "ö"};
String namesOfSons;
public MultiNode(String s, Bintree words, Bintree old, String target){
value=s;
wordlist=words;
oldwords=old;
wanted=target;
makeAdress(adress);
}
public void newSons(){
if(isParent==false){
makeSons();
isParent=true;
}
else{
if(hasSon==true){
for(int i=0; i<sons.length; i++){
sons.newSons();
}
}
}
}
public void makeSons(){
String[] c=value.split("");
String[] v=value.split("");
String test;
for(int i=0; i<c.length-1; i++){
for(int j=0; j<SweAlpha.length; j++){
String[] s={v[1], v[2], v[3]};
s=SweAlpha[j];
test=s[0]+s[1]+s[2];
makeWords(test);
}
}
if(hasSon){
construct(namesOfSons);
}
}
void makeWords(String s){
if(wordlist.hasElement(s)){
if(oldwords.hasElement(s)==false){
if(hasSon==false){
namesOfSons=s;
}
else{
namesOfSons=namesOfSons+" "+s;
}
hasSon=true;
}
}
}
public void construct(String s){
String[] k=s.split(" ");
sons=new MultiNode[k.length];
for(int i=0; i<k.length; i++){
sons=new MultiNode(k, wordlist, oldwords, wanted);
sons.parent=this; ***
oldwords.put(k);
if(k.equals(wanted)){
sons.isTarget=true;
}
}
}
public void makeAdress(Queue q){
if(value.equals(wanted)){
System.out.print(parent.value);
}
try{
parent.makeAdress(q);
}
catch(NullPointerException e){
}
Node n=new Node();
n.value=this;
q.Enque(n);
}
public boolean pathFound(){
boolean b=false;
if(value.equals(wanted)){
b=true;
}
if(hasSon==true){
for(int i=0; i<sons.length; i++){
if(sons.pathFound()==true){
b=true;
}
}
}
return(b);
}
public Queue getPath(){
Queue A=new Queue();
if(isTarget==true){
A=adress;
}
else{
for(int i=0; i<sons.length; i++){
if(sons.pathFound()==true){
A=sons.getPath();
}
}
}
return(A);
}
}