I an parsing a txt file to get data then to plot 2d Gui.
But tons of error, any advice will be appreciated!
// This class is calleed by class
import java.io.*;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.regex.*;
import java.util.StringTokenizer;
public class ParseFile
{
private String Filename="test.txt";
private int x1, y1, x2, y2; // to plot lines from (x1,y1) to (x2,y2)
private int xy[]= new int[206];
public int[] setxy()
{
// parse a file to read line by line;
File file = new File(Filename);
try
{
BufferedReader br = new
BufferedReader(new FileReader(Filename));
String line;
String s1 = "0.5 setlinewidth 0.0 setgray";
String s2 = "stroke clear";
String s3 = "closepath fill";
String[] parts;
int iter = 0;
while ((line = br.readLine()) != null )
{
iter++;
System.out.printf("iterative at %d\n",iter);
line = line.trim();
System.out.println(line);
while (line.equals(s1)) //true
{
System.out.printf("Here String is %s \n",s1);
List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();
while ((line = br.readLine()) != null && !line.equals(s2))
{
line = line.trim();
System.out.println(line);
parts = line.split("\\s+");
for (int j=0;j<parts.length-1;j++)
{
System.out.printf("parts is %s \n",parts[j]);
}
// System.out.printf("Here parts[1] is %s \n",parts[1]);
xList.add(Integer.parseInt(parts[0]));
yList.add(Integer.parseInt(parts[1]));
}
Integer[] x = xList.toArray(new Integer[xList.size()]);
Integer[] y = yList.toArray(new Integer[yList.size()]);
// Plotting here using x and y arrays
int m = xy.length/2-1;
System.out.println("Test in ParseFile.java");
for (int i = 0; i<m-1;i++)
{
xy[i] = x.intValue();
xy[i+m] = y[i].intValue();
System.out.printf("%d\n",xy[i]);
// to return these values to class LinesRectsCirclesJPanel
}
// return xy;
}
while (line.split("\\s+").length == 8)
{
List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();
String[] newpath = line.split("\\s+");
while ((line = br.readLine()) != null && !line.equals(s2))
{
line = line.trim();
parts = line.split("\\s+");
xList.add(Integer.parseInt(parts[0]));
yList.add(Integer.parseInt(parts[1]));
}
Integer[] x = xList.toArray(new Integer[xList.size()]);
Integer[] y = yList.toArray(new Integer[yList.size()]);
// Plotting here using x and y arrays
int m = xy.length/2-1;
xy[0] = Integer.parseInt(newpath[5]);
xy[m+1] = Integer.parseInt(newpath[6]);
for (int i = 0; i<m-1;i++)
{
xy[i+1] = x[i].intValue();
xy[i+m+1] = y[i].intValue();
// to return these values to class LinesRectsCirclesJPanel
}
}
}
}
catch (Exception exception) {
exception.printStackTrace();
}
return xy;
}
}
Here is the parsed txt file.
%!PS-Adobe-3.0 EPSF 3.0
%%BoundingBox: 0 0 612 792
%%Title: Developmental Testing
%%CreationDate: 06/07/2007 08:45:57
%%EndComments
/Helvetica findfont 9 scalefont setfont
0.5 setlinewidth 0.0 setgray
20.0000 20.0000 moveto
592.0000 20.0000 lineto /
592.0000 772.0000 lineto /
20.0000 772.0000 lineto /
20.0000 20.0000 lineto /
stroke clear
22.0000 22.0000 moveto
590.0000 22.0000 lineto /
590.0000 770.0000 lineto /
22.0000 770.0000 lineto /
22.0000 22.0000 lineto /
stroke clear
0.5000 1.0000 0.5000 setrgbcolor newpath 439.5350 127.3837 moveto
423.7250 100.0000 lineto /
423.7250 100.0000 lineto /
415.8200 100.0000 lineto /
415.8200 103.7287 lineto /
421.5722 103.7287 lineto /
435.2295 127.3837 lineto /
closepath fill
0.5 setlinewidth 0.0 setgray
439.5350 127.3837 moveto
423.7250 100.0000 lineto /
415.8200 100.0000 lineto /
415.8200 103.7287 lineto /
421.5722 103.7287 lineto /
435.2295 127.3837 lineto /
439.5350 127.3837 lineto /
stroke clear
0.5000 1.0000 0.5000 setrgbcolor newpath 534.3950 319.0698 moveto
542.3000 305.3779 lineto /
542.3000 305.3779 lineto /
534.3950 291.6861 lineto /
530.0895 291.6861 lineto /
537.9945 305.3779 lineto /
530.0895 319.0698 lineto /
closepath fill
0.5 setlinewidth 0.0 setgray
534.3950 319.0698 moveto
542.3000 305.3779 lineto /
534.3950 291.6861 lineto /
530.0895 291.6861 lineto /
537.9945 305.3779 lineto /
530.0895 319.0698 lineto /
534.3950 319.0698 lineto /
stroke clear