A loop that will read a text file and add the info into nodes and then to a tree

250 Views Asked by At

I am creating a program that reads a text file and sets the info from it into nodes in a ternary tree. I have created the addNode method already but now I am working on the method that will read the imported textile and extract the correct info, set it to the nodes, and build the tree. A text file is given in the following format and is used to create an automated help service system.

Each node has three variable of type String: label, prompt, and message

root                                          //label   
Root Node                                     //prompt   
What Model is the Washing Machine?            //message
root 3           //root signifies the next node to branch, and 3 is the # children
1                                             //label
WM200                                         //prompt
What is the problem?                          //message
2                                             //etc.
WM300                                       
What is the problem?                       
3                                          
WM400                                       
What is the problem?                        
1 3                                         
1-1                                         
No Water.                                   
Is the hose attached?                       
1-2
Water too hot.
Turn the temperature knob to warm.
1-3
Water too cold.
Turn the temperature knob to warm.
1-1 3
1-1-1
Yes, but the hose is broken.
Purchase a new hose.
1-1-2
No, didn't know there was a hose.
Plug it in the back.
1-1-3
Yes, but still no water.
Make sure the water valve is turned on.

Every time there is a complete amount of information (label, prompt, message), I need to add the node to the tree in the correct position (under the parent, left justified)

here is the code I have for the method. (assume the addNode method works correctly and adds nodes from left to right)

the addNode method takes 4 parameters (label, prompt, message, parentLabel)

parentLabel is where the children will be placed. Ex/ root is the parent label in the sample code above at line 4.

while(line != null) {               //while there is still content in the file
    String label = line;            //set label to first line
    line = reader.readLine();
    String prompt = line;           //set prompt to second line
    line = reader.readLine();
    String message = line;          //set message to third line
    line = reader.readLine();

    String parentLabel = "";;
    String numChildren = "";
    if(line.contains(" ")) {
        parentLabel = line.substring(0, line.indexOf(" ")); //parent label up to first space 
        numChildren = line.substring(line.indexOf(" "), line.indexOf("\n")); //number of children from space to end 
    } else
        numChildren = line;     //if no space, line is number of children

    tree.addNode(label, prompt, message, null); //add that node (parentLabel would be null becuase first node)

    for(int i = 0; i < Integer.parseInt(numChildren); i++) {
        line = reader.readLine();
        label = line;           //set label to first line
        line = reader.readLine();
        prompt = line;          //set prompt to second line
        line = reader.readLine();
        message = line;         //set message to third line

        tree.addNode(label, prompt, message, parentLabel); //add node
    }

I think my code is close but it doesn't always work with the patterns given. The text file will always be in that format, however, labels do not have to be in any specific format, and they will not necessarily be made of numbers and dashes.

Any help with this would be greatly appreciated. Thanks

0

There are 0 best solutions below