mcweaver1
January 19th, 2009, 08:41 PM
Hi
Ive created a program which reads in data from a file, each line is of the form <word> <word> representing two nodes with an edge going from the first to the second. It stores and outputs each starter node and the nodes each one is connected to. However I'm having a bit of difficulty with resizing the array...
If anyone can give me a few pointers (har har) I would be very grateful :)
import java.io.*;
import java.util.*;
class Graph
{
private static String strLine = null;
private static String node1 = null;
private static String node2 = null;
private static int initsize = 10;
private static int numelements = 0;
void readfile (String filename) throws FileNotFoundException
{
try
{
Vertex[] edge = new Vertex[initsize];
for(int i=0; i<initsize; i++)
{
edge[i] = new Vertex();
}
BufferedReader reader = new BufferedReader(new FileReader(filename));
while ((reader.ready())&&((strLine = reader.readLine()) != null))
{
// splits each line into seperate words at a "\\s"
Scanner scanner = new Scanner(strLine);
scanner.useDelimiter(" ");
//whilst still words left in the file to read in
if (scanner.hasNext())
{
//set node1 as the word before the "\\", set node2 as the word after
node1 = (scanner.next());
node2 = (scanner.next());
}
for (int j=0; j<edge.length; j++)
{
if (j==edge.length)
{
resize(edge, edge.length);
}
if (edge[j].startnode == null)
{
edge[j].startnode = node1;
edge[j].endnodes.add(node2);
break;
}
else if (edge[j].startnode.equals(node1))
{
edge[j].endnodes.add(node2);
break;
}
}
//edge.length DOES NOT ouput the right number of elements in the array here
System.out.println("Node1 = " + node1 + " ... node2 = " + node2);
}
print(edge, edge.length);
}
catch(IOException e)
{
}
}
private void print (Vertex[] edge, int length)
{
System.out.println();
for (int i=0; i<length; i++)
{
if (edge[i].startnode == null)
{
break;
}
System.out.println();
System.out.print("Node " + edge[i].startnode + " links to nodes ");
for (int j=0; j< edge[i].endnodes.size(); j++)
{
System.out.print(edge[i].endnodes.get(j));
if (!(j==(edge[i].endnodes.size()-1)))
System.out.print(",");
}
}
System.out.println();
}
/*public Graph()
{
}*/
private Vertex[] resize (Vertex[] edge, int length)
{
Vertex[] newEdge = new Vertex[length*2];
for (int i=0; i<length*2; i++)
{
newEdge[i] = new Vertex();
//newEdge[i] = edge[i];
}
System.arraycopy (edge, 0, newEdge, 0, length);
edge = newEdge;
return edge;
}
public static void main(String arg[]) throws FileNotFoundException
{
if (arg.length!=2)
{
System.err.println("Wrong input, two arguments needed");
System.exit(1);
}
Graph program = new Graph();
program.readfile(arg[1]);
}
}
Ive created a program which reads in data from a file, each line is of the form <word> <word> representing two nodes with an edge going from the first to the second. It stores and outputs each starter node and the nodes each one is connected to. However I'm having a bit of difficulty with resizing the array...
If anyone can give me a few pointers (har har) I would be very grateful :)
import java.io.*;
import java.util.*;
class Graph
{
private static String strLine = null;
private static String node1 = null;
private static String node2 = null;
private static int initsize = 10;
private static int numelements = 0;
void readfile (String filename) throws FileNotFoundException
{
try
{
Vertex[] edge = new Vertex[initsize];
for(int i=0; i<initsize; i++)
{
edge[i] = new Vertex();
}
BufferedReader reader = new BufferedReader(new FileReader(filename));
while ((reader.ready())&&((strLine = reader.readLine()) != null))
{
// splits each line into seperate words at a "\\s"
Scanner scanner = new Scanner(strLine);
scanner.useDelimiter(" ");
//whilst still words left in the file to read in
if (scanner.hasNext())
{
//set node1 as the word before the "\\", set node2 as the word after
node1 = (scanner.next());
node2 = (scanner.next());
}
for (int j=0; j<edge.length; j++)
{
if (j==edge.length)
{
resize(edge, edge.length);
}
if (edge[j].startnode == null)
{
edge[j].startnode = node1;
edge[j].endnodes.add(node2);
break;
}
else if (edge[j].startnode.equals(node1))
{
edge[j].endnodes.add(node2);
break;
}
}
//edge.length DOES NOT ouput the right number of elements in the array here
System.out.println("Node1 = " + node1 + " ... node2 = " + node2);
}
print(edge, edge.length);
}
catch(IOException e)
{
}
}
private void print (Vertex[] edge, int length)
{
System.out.println();
for (int i=0; i<length; i++)
{
if (edge[i].startnode == null)
{
break;
}
System.out.println();
System.out.print("Node " + edge[i].startnode + " links to nodes ");
for (int j=0; j< edge[i].endnodes.size(); j++)
{
System.out.print(edge[i].endnodes.get(j));
if (!(j==(edge[i].endnodes.size()-1)))
System.out.print(",");
}
}
System.out.println();
}
/*public Graph()
{
}*/
private Vertex[] resize (Vertex[] edge, int length)
{
Vertex[] newEdge = new Vertex[length*2];
for (int i=0; i<length*2; i++)
{
newEdge[i] = new Vertex();
//newEdge[i] = edge[i];
}
System.arraycopy (edge, 0, newEdge, 0, length);
edge = newEdge;
return edge;
}
public static void main(String arg[]) throws FileNotFoundException
{
if (arg.length!=2)
{
System.err.println("Wrong input, two arguments needed");
System.exit(1);
}
Graph program = new Graph();
program.readfile(arg[1]);
}
}