Results 1 to 2 of 2
- 07-17-2007, 03:45 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 26
- Rep Power
- 0
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:
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).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());
The output appears as expected. Perfect.
Now, I would like to issue those commands through an external file.
The commands would be as follows:
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:u = pen up d = pen down n10 = move up 10 spaces e100 = move right 100 spaces ... ... etc.
Now, in that while loop I will check each line (i was thinking of using a switch/case) to call a certain method.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"); }
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:
ThanksJava Code:u d n13 s10 e3 w9 u e3 n5 d s7 u q
- 08-07-2007, 05:28 AM #2
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
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
Sample Java codeJava Code:U D N-13 S-10 E-3 W-9
Greetings.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; } }
Similar Threads
-
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
how to use jwnl.. i run a sample program it will ask properties file.. what can i do
By sathishk in forum New To JavaReplies: 1Last Post: 02-16-2008, 03:33 AM -
Poblem in Compiling a c++ file from a java program...
By Amit Kr. Mishra in forum New To JavaReplies: 0Last Post: 11-06-2007, 10:41 AM -
External Program execution problems
By vital101 in forum Advanced JavaReplies: 3Last Post: 10-30-2007, 05:17 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks