I have created a program where the user draws a series of vertices and associated edges on screen. This part of the program works with no problems. However, when I try to use StringTokenizer to read the data back from a file, the graph is not drawn properly. The vertices and edges are correctly added to their relevant vectors, and everything seems to be normal, however only the vertices are drawn on screen.
This only happens when I open a file, when I draw the edges manually, they are displayed correctly. When an edge is to be drawn, it first has to search the vector of vertices to get the corresponding x and y co-ordinates. I am guessing that it is at this part of the program I am getting an error.
I can fix this problem using arrays, however it is part of a project I am doing and I am required to use vectors. If anyone could help I would be extremely grateful, Thanks.
Below is the code where I read from file:
publicvoid openFile()// open a file
{ final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(MenuFrame.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
try
{
Reader dataReader;
dataReader = new FileReader(file.getName());
BufferedReader reader = new BufferedReader(dataReader);
String inputLine;
while ( (inputLine = reader.readLine()) != null )
{ StringTokenizer tokenizer = new StringTokenizer(inputLine);
String token;
token = tokenizer.nextToken();
if (token.equals("Vertex"))
{ String vertexName = tokenizer.nextToken();
addVertex(vertexName,
(Integer.parseInt(tokenizer.nextToken())),
(Integer.parseInt(tokenizer.nextToken())));
}
elseif (token.equals("Edge"))
{ String from = tokenizer.nextToken();
String to = tokenizer.nextToken();
addEdge(from, to, ( Integer.parseInt(tokenizer.nextToken())));
}
}
}
Here is the code where I draw the vertices and edges: