Results 1 to 2 of 2
  1. #1
    romina is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default Use a external file in my program

    I have just created a simple class that can "draw" integers on 20x20 2d array. (some of you may be familiar with the Turtle.java assignment, well, I'm just making a really simple version of that right now).

    Currently I issue commands through the main method as follows:
    Java Code:
          Turtle Romina = new Turtle();
            Romina.setStage();
            Romina.moveEast(5);
            Romina.moveNorth(5);
            Romina.setPenState("u");
            Romina.moveWest(5);
            Romina.setPenState("d");
            Romina.moveSouth(5);
            System.out.println(Romina.getArray());
    this would output somthing like a 20x20 array of 0's with a square in the middle drawn with 1's (excluding the top part of the square because the "pen" is lifted during that part).

    The output appears as expected. Perfect.
    Now, I would like to issue those commands through an external file.
    The commands would be as follows:
    Java Code:
    u = pen up
    d = pen down
    n10 = move up 10 spaces
    e100 = move right 100 spaces
    ...
    ...
    etc.
    I can read from the external file, but I am not sure how to handle the data. Currentley I wrote this bit of code that breaks the file down by line:
    Java Code:
            try {
                /**Read the specified file to give the turtle Directions*/
                BufferedReader turtleBrain = new BufferedReader(new FileReader("C:\\iTurtle.txt"));
                
                /**Seperate the file by line*/
                String thought;
                while ((thought = turtleBrain.readLine()) != null){
                    //just for testing
    		System.out.println(thought);
                }
                
            } catch (FileNotFoundException ex) {
                System.out.println("File not found");
            } catch (IOException ex){
                System.out.println("IO ERROR");
            }
    Now, in that while loop I will check each line (i was thinking of using a switch/case) to call a certain method.
    i.e. case "u" will call setPenState with the parameter "u".
    The above is pretty straight forward, I don't have any trouble with that... What I would like some guidance on are the movements:

    for example:
    the line read = n8
    I will have: case "n" that calls moveNorth and sends it the parameter "8".

    Right now I a messing around with grabbing the line as a string, remove and check the first charater using RegEx, and if the first char is n, s, e, or w (North, South, East, West) parseInt the following number and send it to the appropriate method.
    I have a feeling there is a better way to do this.

    The iTurtle.txt file would contain somthing like:
    Java Code:
    u
    d
    n13
    s10
    e3
    w9
    u
    e3
    n5
    d
    s7
    u
    q
    Thanks

  2. #2
    trill is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    Why not put a character or space in between the command and parameter? That way you can utilize Java's Split function and do something like the below.

    iTurtle.txt

    Java Code:
    U
    D
    N-13
    S-10
    E-3
    W-9
    Sample Java code

    Java Code:
    enum Commands {U, D, N, S, E, W};
    String thought;
    while ((thought = turtleBrain.readLine()) != null){
      String[] instructions = thought.split("-");
      Commands com = instructions[0];
      switch (com) {
        case U: Romina.setPenState("u"); break;
        case D: Romina.setPenState("d"); break;
        case N: Romina.moveNorth(instructions[1]); break;
        case S: Romina.moveSouth(instructions[1]); break;
        case E: Romina.moveEast(instructions[1]); break;
        case W: Romina.moveWest(instructions[1]); break;
        default: System.out.println("Invalid Command"); break;
      }
    }
    Greetings.

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  2. Replies: 1
    Last Post: 02-16-2008, 03:33 AM
  3. Poblem in Compiling a c++ file from a java program...
    By Amit Kr. Mishra in forum New To Java
    Replies: 0
    Last Post: 11-06-2007, 10:41 AM
  4. External Program execution problems
    By vital101 in forum Advanced Java
    Replies: 3
    Last Post: 10-30-2007, 05:17 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •