String Tokenizer?
I have a line of text Formatted in the following sequence
for lines
Line [x sub1, y sub1][x sub2, y sub2]
for Ovals and Rectangles
Shape_type [x-coor, y-coor][height,width]
I need to parse this string into an array of MyShapes that holds starting X
and Y and ending X and Y which are used in sub-classes MyOval, and MyRectang
le to calculate height, and width. MyShapes is abstract and the sub-classes
are concrete. I got as far as reading the number of shapes to be created f
rom the file but I cant figure out how to read in the values to the appropri
ate fields. It works fine if the values for x1, y1, x2, and y2 are generate
d within the program.
sample lines of text from file being read
Oval [318,279][217,198]
Line [138,106][200,202]
Rectangle [219,59][166,64]
Oval [169,106][209,65]
Rectangle [290,320][285,98]
so in effect i want to be able to read this from a file and assign it to
MyShapes[] shapes = new MyShapes();
for (i =0; i<numShapes; i++){
first make shapes an instance of shape_type
shapes.x1 = x1 read from file
shapes.y1 = y1 read from file
if shapes instanceof MyLine{
shapes.x2 = x1 read from file
shapes.y2 = y2 read from file
}else{
shapes.h = h read from file
shapes.w = w read from file
}
the main problem im having is figuring out how exactly to extract the data f
rom the file. Im pretty sure i need to use string tokenizer, but i cant get
it to work right.>
[1664 byte] By [
danTura] at [2007-10-2 6:36:12]

Haven't tested it so I bet there's mistakes in there, but this is along the lines of what you want...
String whitespace = "\\s*";
String type = "([^\\s])";
String number = "(\\-?+[0-9])";
String pair = "\\[" + whitespace + number + whitespace + "," + whitespace + number +whitespace + "]";
String line = "^" + whitespace + type + whitespace + pair + whitespace + pair + whitespace + "$";
Pattern p = Pattern.compile(line);
Matcher m = pattern.matcher(nextLine);
String shapeType = m.group(1);
int x1 = Integer.parseInt(m.group(2));
int y1 = Integer.parseInt(m.group(3));
int x2 = Integer.parseInt(m.group(4));
int y2 = Integer.parseInt(m.group(5));
I need to change the input file to read like this
shapeType COLOR [x1,y1][x2,y2] //for lines
shapeType COLOR [x,y][h,w]//for rectangles and ovals
the color field needs to be used in the following manner inside my paint method
Color c = "value read in from each line of file"
Graphics g;
g.setColor(c);
g.draw();
i added a declaration that reads the following
String color = "([^\\s])"
and adjusted the declaration for "String line..." to read like this
String line = "^" + whitespace + type + whitespace + color + whitespace + pair + whitespace + pair + whitespace + "$";
i also added in the parsing part of the routine that reads
String shapeColor = m.group(2);
and incremented the remaining group numbers by 1
Im trying to set the color of each shape before it is drawn by changing the value of c to the value of the shapeColor string that is in all CAPS.
1)Did I adjust the parser to properly extract the color portion of each line into shapeColor?
2)How do I chage the shapeColor String into a valid value to construct a Color object c?