I am writing a program for school, its a problem from Deitel and Deitel Java: How to program. I have to write a turtle drawing program.

It works great, what I have now is a method named enterCommands() that works that takes no arguments that can be used to cycle through input for a series of integers that will be used in the executeCommands() method. All I need now is a way to use a string that is passed into the overloaded method executeCommands(String theString) that can be cycled through with an enhanced for loop and used to assign numeric values of the string into an array list that can be used with switch(command) in the executeCommands() method. I have tried many times but I cannot get anything to work. Any help would be appreciated.
Here is my code.

This is my class for testing that contains a main method:

import java.io.File;
import java.util.Scanner;
/**
 * Write a description of class TurtleGraphicsTest here.
 */
public class TurtleGraphicsTest
{
/**
 * @param  args not used   
 * @return      
 */
public static void main( String[] args ) throws Exception
{   
    TurtleGraphics drawing = new TurtleGraphics(); // instantialize our TurtleGraphics object
    File file = new File("input.txt");
    Scanner infile = new Scanner(file);

    drawing.enterCommands(); // enter commands by prompt

    drawing.enterCommands(infile.nextLine()); 
    // enter commands by passing string, in this case, data saved in input.txt
}
}

Here is the class of importance:

import java.util.Scanner;
import java.util.ArrayList;

public class TurtleGraphics
{
  private final int FLOOR_Y_SIZE = 20;
  // amount of rows on the floor 
  private final int FLOOR_X_SIZE = 20;
  // amount of columns on the floor
  private final int PEN_UP = 1; 
  // not ready to draw
  private final int PEN_DOWN = 2; 
  // ready to draw
  private final int TURN_RIGHT = 3;
  // turtle turns clockwise
  private final int TURN_LEFT = 4;
  // turtle turns counter clockwise
  private final int TURTLE_MOVE = 5; 
  // move the turtle
  private final int DISPLAY = 6;
  // display drawing
  private final int CLEAR = 7; 
  // clear the floor
  private final int REPOSITION = 8;
  // start the turtle back at the beginning, and face east
  private final int END = 9;
  // end input

  private boolean DONE = false;
  private int[][] floor = new int[FLOOR_Y_SIZE][FLOOR_X_SIZE];
  private int posy = 0, posx = 0; // starting position of turtle

  private int command;
  // holds the current command to be processed by switch(commands)
  // in executeCommands method
  private int spaces;
  // holds the amount of spaces the 
  // turtle will go when is moving
  private String decal = "*"; 
  // this can be changed to designate what
  // symbol will be drawn on the floor

  private static enum Direction { NORTH, SOUTH, EAST, WEST};
  // enum to contain all of the possible direcitons of our turtle
  private static enum penStatus {UP, DOWN};
  // enum for the status of the pen. It can either be UP, or DOWN

  private penStatus pen = penStatus.UP; 
  // class starts with turtle not ready to draw
  private Direction direction = Direction.EAST; 
  // class starts with turtle facing east

 /**
 *  Enter a series of commands.
 *  Do not have to enter command and press enter each time.
 *  Entire sequence of commands may be entered on one line
 *  and then executed. Anything after 5, with or without comma
 *  will be the amount to move the turtle forward.
 */
  public void enterCommands()
  {
     DONE = false;
     Scanner input = new Scanner( System.in );
     input.useDelimiter("(\\s)+|,(\\s)*");
     instructions();
     System.out.printf("Enter series of commands:");

     while( !DONE ) //ends when user enters "9"
     {
        command = input.nextInt();

        if (command == 5)
           spaces = input.nextInt();

        executeCommands();

     }  
  }

 /**
 *  Enter a series of commands passed to the method by string.
 *  Can be a file or a string literal passed to this method
 *  defined by the user or passed by command-line argument
 * @param  theString From file, command-line argument or literal  
 */
  public void enterCommands(String theString)
  {
   ArrayList< String > commands = new ArrayList< String >();

    for (char command : theString.toCharArray() )
    {
    }
    System.out.printf("Commands entered: %s",commands);
  }

  public void executeCommands()
  {
     switch(command)
     {
        case PEN_UP: 
           pen = penStatus.UP; // not ready to draw!
           break;
        case PEN_DOWN:
           pen = penStatus.DOWN; // ready to draw!
           break;
        case TURN_RIGHT: // allows full clockwise turn
           if (direction == Direction.WEST)
              direction = Direction.NORTH;
           else if (direction == Direction.NORTH)
              direction = Direction.EAST;
           else if (direction == Direction.EAST)
              direction = Direction.SOUTH;
           else if (direction == Direction.SOUTH)
              direction = Direction.WEST;
           break;
        case TURN_LEFT: // allows full counter-clockwise turn
           if (direction == Direction.WEST)
              direction = Direction.SOUTH;
           else if (direction == Direction.SOUTH)
              direction = direction.EAST;
           else if (direction == Direction.EAST)
              direction = direction.NORTH;
           else if (direction == Direction.NORTH)
              direction = direction.WEST;
           break;
        case TURTLE_MOVE:
           if (direction == Direction.WEST)
           { 
              for (int count = 1; count <= spaces; count++)
              {
                 if(pen == penStatus.DOWN)
                    floor[posy][posx] = 1;
                    // mark this location to be drawn
                 if (posx > 0) posx--;
                 //allows movement if the pen is not ready to draw
                 // and will not exceed the bounds of the floor
              }
           }
           else if (direction == Direction.EAST)
           {
              for (int count = 1; count <= spaces; count++)
              {
                 if(pen == penStatus.DOWN)
                    floor[posy][posx] = 1;
                    // mark this location to be drawn
                 if (posx < FLOOR_X_SIZE - 1) posx++;
                 //allows movement if the pen is not ready to draw
                 // and will not exceed the bounds of the floor
              }
           }
           else if (direction == Direction.NORTH)
           {
              for (int count = 1; count <= spaces; count++)
              {
                 if (pen == penStatus.DOWN)
                    floor[posy][posx] = 1;
                    // mark this location to be drawn
                 if (posy > 0) posy--; 
                 //allows movement if the pen is not ready to draw
                 // and will not exceed the bounds of the floor
              }
           }
           else if (direction == Direction.SOUTH)
           {
              for (int count = 1; count <= spaces; count++)
              {
                 if (pen == penStatus.DOWN)
                    floor[posy][posx] = 1;
                    // mark this location to be drawn
                 if (posy < FLOOR_Y_SIZE - 1) posy++;
                    //allows movement if the pen is not ready to draw
                 // and will not exceed the bounds of the floor
              }
           }
           break;
        case DISPLAY: // show the floor
           display();
           break;
        case CLEAR: // this clears the floor
           floor = new int[FLOOR_Y_SIZE][FLOOR_X_SIZE];
           break;
        case REPOSITION: // move back to starting point facing EAST, and bring the pen up
           posx = 0;
           posy = 0;
           direction = Direction.EAST;
           pen = penStatus.UP;
           break;
        case END: // finished entering commands
           DONE = true;
           break;
        default: System.out.printf("Not an appropriate command: %s\n", command);
     }
  }

/**
 *  Cycle through and display what we have drawn on our 20x20 floor
 * 
 */
  private void display()
  {
     // loop through the rows
     for ( int y = 0 ; y < FLOOR_Y_SIZE; y++) 
     {
         // loop through the columns
        for (int x = 0; x < FLOOR_X_SIZE  ; x++)
        {
           // if there is a 1 in this location, draw the decal
           if (floor[y][x] == 1) 
              System.out.printf("%s", decal);
           else
              System.out.printf(" ");
        }
        System.out.println(); 
        // go to next line when finished with current row
     }
  }

  private void instructions()
  {
     System.out.printf("Instructions:\n");
     System.out.printf("1 Pen UP\n");
     System.out.printf("2 PEN DOWN\n");
     System.out.printf("3 TURN RIGHT\n");
     System.out.printf("4 TURN LEFT\n");
     System.out.printf("5 MOVE THE TURTLE A GIVEN AMOUNT OF SPACES");
     System.out.printf("Ex. 5,10 moves forward 10 spaces\n");
     System.out.printf("6 DISPLAY THE FLOOR\n");
     System.out.printf("7 CLEAR THE FLOOR\n");
     System.out.printf("8 REPOSITION THE TURTLE AT START LOCATION\n");
     System.out.printf("9 STOP ENTERING COMMANDS\n"); 
     System.out.printf("SAMPLE COMMAND SEQUENCE:\n");
     System.out.printf("5,6 2 5,6 3 5,6 4 5,6 3 5,6 3 5,6 4 5,6"); 
     System.out.printf(" 3 5,6 3 5,6 4 5,6 3 5,6 3 5,6 4 5,6 6\n");
    }
}

This is a sample command sequence that I saved in a file named input.txt

5,6 2 5,6 3 5,6 4 5,6 3 5,6 3 5,6 4 5,6 3 5,6 3 5,6 4 5,6 3 5,6 3 5,6 4 5,6 6

Use String's split method to get a String[] (which you can then use in the "enhanced" for loop) then use Integer.parse(String) to get the integer values for the switch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.