Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-17-2007, 04:45 PM
Member
 
Join Date: Jul 2007
Posts: 26
romina is on a distinguished road
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:
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:
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:
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:
Code:
u d n13 s10 e3 w9 u e3 n5 d s7 u q
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 06:28 AM
Member
 
Join Date: Jul 2007
Posts: 40
trill is on a distinguished road
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

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

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
how to use jwnl.. i run a sample program it will ask properties file.. what can i do sathishk New To Java 1 02-16-2008 04:33 AM
Poblem in Compiling a c++ file from a java program... Amit Kr. Mishra New To Java 0 11-06-2007 11:41 AM
External Program execution problems vital101 Advanced Java 3 10-30-2007 06:17 PM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM


All times are GMT +3. The time now is 09:36 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org