Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Questions on HW6PART1-Spring08

Question: I'm having problems with my toString method, when I get the commands it is adding an extra 0 to the commands that only have on value and I don't understand why?

Question: What is the text file for adding more commands?

Question: I'm getting a NullPointerException when I try to do commands.add(aNewCommand) in my extractCommands() method. I've added
REMOVEDkedList<TurtleCommand> commands = new REMOVEDkedList<TurtleCommand>();
to the constructor as per the instructions. What could be causing this?

My extractCommands() keeps skipping the first line for some reason. I'm using BufferedReader, FileReader, and StringTokenizer in a way that's worked for me before. Might this have something to do with using StringTokenizer(line), with no delimiter in the constructor?
Here's the code associated with the above question:
BufferedReader input = new BufferedReader( new FileReader( turtleCommands ) );
      String line = input.readREMOVEDe(); //(1)
      while ( (line = input.readREMOVEDe()) != null ) { //(2)
        StringTokenizer mac = new StringTokenizer(line);
        String type = mac.nextToken().trim(); //get command type
        TurtleCommand command = new TurtleCommand(type); //make a new TurtleCommand

In reference to the above question, isn't the delimiter a space? Hence StringTokenizer(line, " "); or does StringTokenizer use white space as the default delimiter?

I have the code:
try {
      BufferedReader br = new BufferedReader(new FileReader(turtleCommands));
      String line = br.readREMOVEDe();
      int count = 0;
      while( (count < 1000) &amp; (br.readREMOVEDe()) != null) {
        StringTokenizer st = new StringTokenizer(line, "\t");
        String commandType = st.nextToken().trim();

When I println'd in an attempt to debug as stated above, i got "World 400 400" printed out a bunch of times in the interactions pane. I tried adding:

int param1 = Integer.parseInt(st.nextToken().trim());
        int param2 = Integer.parseInt(st.nextToken().trim());

to try and get the tokens after world, but that just ended up printing nothing in the interactions pane, and I do not know why.

If using a scanner how do you access the first token - I only see methods for .next()

How do you save the commands from commands REMOVEDkedList to the incoming file in the outputTo method? thanks i keep getting errors

In order to draw the commands, how can we loop through the REMOVEDkedList of TurtleCommands without a next/getNext() variable/method in the TurtleCommands class?

I'm using the following code:

Scanner scan = new Scanner(turtleCommands); // reads in the file 
      scan.useDelimiter("\t"); // uses a tab as the delimiter
      String input;
      while(scan.hasNext()) {  // while the file has a next token
        input =;
        System.out.println(input);  // test the output

And cannot figure out why when I run the main method, it is printing out "TurtleDrawing.txt." twice and that's all. I've tried looking through the Scanner API and such, but cannot figure out how to just get it t return the tokens from the text file. I have all of the BufferedReader/FileReader stuff before I start the Scanner.

Can we assume that the first line in every text file will be a "world" command?

In the TurtleDrawing Method, i am getting the error that the method "forward" cannot be found in this class which makes sense because this method does not extend the Turtle class. How will i be able to use the "Turtle" class' methods without extending the class?

I'm not sure how to even start writing my toString method, and I know Joel and Dawn said it was important for debugging the rest of the code. I've been using println statements fairly liberally to debug, though. Can you lead (or shove?) me in the write direction for starting the toString, beyond what it outlined in the HW 6 pdf

for the addMoreCommandsFromFile(string moreCommands) method - should we assume it will always be called before drawCommands()? - mine just calls extractCommands and it works but when I call addmorecommandsfromfile after the drawCommands in the main method I get a nullpointer exception

when I call morecommandsfromfile all I do is call the extract method again - but it doesn't work and I don't understand why b/c wouldnt that just append the commands list

also when we call the morecommandsfromfile method it should be called by a turtledrawing right?

when I run my draw commands method - the world appears and the commands are drawn but then it never stops running or doesnt't go on to the other methods

also regarding the outputtofile method - the following code creates the file but then nothing else happens and it never stops running - I dont even get anything in the file like the line right after the file is created

public void outputToFile(String outputFile) throws,{
try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
        TurtleCommand node=commands.get(0);
        int ctr = 0;
        String result = "";
        while(node !=null){
          result = node.getType() +" ";
          if (node.getParameters().size() ==1){
           if (node.getParameters().size() >1){
         bw.write(result+node.getParameter(0)+" "+node.getParameter(1));
    } catch (IOException e) {

Since I can't use a switch statement on the type because it's a String, I'm using an if statement. However, if I refer to the turtle that I know I created from the first line of the text file, Java tells me that it may not have been initialized (and I guess it's right). Can I make a new turtle outside the if statement, since we know for sure that "world" will be on the first line of the text file?

This is my toString method but for some reason even when its the only line of code in the main method nothing happens

public String toString()  
    String result = "";
    int ctr=0;
    TurtleCommand node = commands.get(ctr);
    while(node != null){
      result = result + node.toString() + "\n";
  return result;

in the main method i have this:

is what I have in the main method not a command to print?

are you saying that the logic is incorrect and will not produce anything to print or that I am just not printing what I have? - if you mean adding a println statement right before the return result line I already tried that and still nothing.

ok well obviously I am not catching whatever it is im doing wrong - can you maybe help me figure it out? I have been working on this problem for almost two hours...

for some reason this method will not exit the for loop - both result and the commands list will print out perfectly if I do it inside the for loop but even if its just one line outside NOTHING happens - what is happening?????

public String toString()  
    String result = "";
    int ctr=0;
    TurtleCommand node = commands.get(ctr);
    for(int i=0; i < commands.size(); i++)
      result = result+ node.toString() + "\n";


I wrote a output method that works correctly using printWriter. Is that ok for the homework assignment? thanks.

Does only one partner turn the homework in?

Link to this Page