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? |
- Hard to tell what could be wrong without looking at the code. Dawn Finney.
- Only add the second parameter if it is needed. Be sure not to add an extra parameter for forward, turn, or any of the ones that only take 1 parameter. Joel Uthe
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
- The problem is that you declare line to be the first line of the file in the first line of code (1) and then tell it to become the next line of the file in the while conditional(2). This way you will never tokenize the first line. Dawn Finney
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?
- StringTokenizer uses whitespace as the default delimiter (including spaces, tabs, and newlines). But the problem is that the above code takes the first token in line 1 but then never processes it. When it gets to the while loop you are one token further then you would think. Try using println statements to debug the code to see if what you are storing is what you expect. Joel Uthe
- The delimiter is actually a tab so "\t" Dawn Finney
I have the code:
try {
BufferedReader br = new BufferedReader(new FileReader(turtleCommands));
String line = br.readREMOVEDe();
int count = 0;
while( (count < 1000) & (br.readREMOVEDe()) != null) {
count++;
StringTokenizer st = new StringTokenizer(line, "\t");
String commandType = st.nextToken().trim();
System.out.println(line);
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.
- You never move to the next line. Look more carefully at your code and remove that ridiculous count crap. Dawn Finney.
If using a scanner how do you access the first token - I only see methods for .next()
- next() gives you the next token. Dawn Finney
- The first time you call .next() it will give you the first token. Joel Uthe
How do you save the commands from commands REMOVEDkedList to the incoming file in the outputTo method? thanks i keep getting errors
- Use FileWriter. It does almost everything for you. If the file specified does not exist, FileWriter creates it for you. Dawn Finney.
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 = scan.next();
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.
- Try Scanner scan = new Scanner(new File(turtleCommands)); Dawn Finney.
Can we assume that the first line in every text file will be a "world" command?
- Technically I said that you could assume this for the mini homework but it isn't true anymore if you are using the same method to extract the extra commands from the moreCommands.txt. It's probably easier in the long run not to assume this. Dawn Finney.
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?
- There is a Turtle already defined in TurtleDrawing (look at the global variables). I believe his name is turtle. So you will need to instantiate him with whatever World you create and use him to do your drawing. Joel Uthe
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
- The toString() method should return a String that is the compilation of all the TurtleCommands combined in your REMOVEDkedList<TurtleCommands> commands. Try just running through the linked list and printing out each element. If that works then think about how you can combine each of those items into one long String that you can return to the caller. Joel Uthe
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
- No you cannot assume it will always be called before drawCommands. Look at the line number that the exception is occurring and trace it back to where that object was set to null. Joel Uthe
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
- It should just append the new commands onto the old one. Why it doesn't? Well the answer is in your code. Joel Uthe
also when we call the morecommandsfromfile method it should be called by a turtledrawing right?
turtledrawing.morecommands(morecommands.txt)
- It should be called from an instance of TurtleDrawing. If your instance is called turtledrawing, then yes that is the correct way to call it. Remember how we could name our Turtles anything we wanted (fred, bob, turtle, susy)? You can also call your instances of TurtleDrawing anything you want. My favorite is TurtleDrawing michaelangelo = new TurtleDrawing("initialCommands.txt"). Then to add more commands I would call michaelangelo.addMoreCommandsFromFile("moreCommands.txt"); Joel Uthe
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 java.io.FileNotFoundException, java.io.IOException{
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
bw.write("working");
TurtleCommand node=commands.get(0);
int ctr = 0;
System.out.println("working");
String result = "";
while(node !=null){
System.out.println("working");
result = node.getType() +" ";
bw.write(result);
if (node.getParameters().size() ==1){
bw.write(result+node.getParameter(0));
bw.write("\n");
}
if (node.getParameters().size() >1){
bw.write(result+node.getParameter(0)+" "+node.getParameter(1));
bw.write("\n");
}
ctr++;
node=commands.get(ctr);
System.out.println("working");
}
bw.close();
System.out.println("working");
} catch (IOException e) {
e.printStackTrace();
}
- You are creating way too much work for yourself. If you have your toString method creating the right format then your outputToFile method should only be 3-4 lines of code. Dawn Finney.
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?
- Your turtle is declared as a global variable (declared outside of all of the methods at the top of the class definition) for you already. Just use that one. Instantiate him with the first command which is world. Dawn Finney.
- Sometimes this error occurs when you instantiate your turtle in an if loop, but not the else part. Try adding turtle=null in your constructor. Joel Uthe
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";
ctr++;
node=commands.get(ctr);
}
return result;
}
in the main method i have this:
System.out.println(td.toString());
- Why would the toString method work if there are no commands to print? Dawn Finney.
is what I have in the main method not a command to print?
- But what should be printing out if there is nothing to print? Dawn Finney.
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...
- Take a break. Drink a beer (if you are 21). And reevaluate the problem. There may be something a little bit off about your initial thought process that no amount of tinkering will fix.
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()
{
System.out.println("working");
String result = "";
int ctr=0;
System.out.println("working");
TurtleCommand node = commands.get(ctr);
for(int i=0; i < commands.size(); i++)
{
result = result+ node.toString() + "\n";
//System.out.println(node.toString());
ctr++;
node=commands.get(ctr);
}
System.out.println(commands.toString());
//System.out.println(result);
return(result);
}
I wrote a output method that works correctly using printWriter. Is that ok for the homework assignment? thanks.
- FileWriter was just a suggestion. Using a similar class that works is okay. Dawn Finney.
Does only one partner turn the homework in?
- Yes, only one partner has to turn the homework in. If both people turn it in then make sure that it is the same version because the TA's will choose a random one to grade. Joel Uthe
Link to this Page