Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

Ken Ng
Greenhorn
Posts: 26
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,
skip(), mark(), reset()..??
Confused, overwhelmed, no idea how to use them
What is the best way to achieve this?

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
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
Hi james,
Thanks for the tips