Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading and skipping data...confused

 
Ken Ng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone help me with this problem?
I have a file which has the following contents,
_______________________________________________
shape1
300 190
280 560
455 100

shape2
120 140
120 190
360 140
670 190

shape3
:
:
_______________________________________________
The set of numbers are actually coordinates of the respective shapes.
I want to retrieve ONLY those sets of coordinates and the number of sets corresponding to a particular shape and assign to an array, , e.g for shape1,

no_of_set[0] = 3
x[0] = 300 y[0] = 190
x[1] = 280 y[1] = 560
x[2] = 455 y[2] = 190

NOT those words shape1,2... and those empty newlines.
How to do that?
I thought of several ways which "seems" can do the job,
FilterReader, StreamTokenizer, DataInputStream, LineNumberReader..?
skip(), mark(), reset()..??
Confused, overwhelmed, no idea how to use them
What is the best way to achieve this?
Pls HELP! Thanks in adv
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd start with an easy buffered reader that can read a line at a time and just print them out. Look at the lines coming through to make sure you know the "syntax" of the file. Looks like the pattern is: shape name, blank line, x y (some number of times), blank line. If that's right, you need something like

Here's one way to split a line:
String[] myPoint = myLine.split(" ");
That gives you a String array for each point. myPoint[0] is X, myPoint[1] is Y.
Now, how to store all the points for one shape? You suggested an array but you have to know the size when you declare an array, and different shapes migh have different numbers of points. An ArrayList is open ended. You don't have to know how many points you'll have. So start out with
Finally, we have to store each shape in some kind of structure so we can come back and use them later. A Map would let you retrieve shapes by name. So we wind up with something like this:

Did that make sense? You might see how much of it you can make in code and give us a progress report. Are Map and List and all those classes new to you? Try the JavaDoc and let us know if you need help using them.
 
Ken Ng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi james,
Thanks for the tips
I have tried to follow close to your suggested way.
I need to test whether my program is correct by printing out the map's keys and it's corresponding values, but I'm not familiar with maps and lists.
How to do that?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!