IN JAVA how do I do I grab this information from the text file and put it into m
ID: 3804651 • Letter: I
Question
IN JAVA how do I do I grab this information from the text file and put it into my Edge[] ?
I have a program the creates a directed weighted graph and it implements Dijktras algorithm. How I add do this is by hard coding the vertex edge and weight like this
[code]
The output is
"Number of nodes = 8
Number of edges = 14
The shortest distance from node 0 to node 0 is 0
The shortest distance from node 0 to node 1 is 3
The shortest distance from node 0 to node 2 is 1
The shortest distance from node 0 to node 3 is 4
The shortest distance from node 0 to node 4 is 2
The shortest distance from node 0 to node 5 is 4
The shortest distance from node 0 to node 6 is 8
The shortest distance from node 0 to node 7 is 4"
I have to now make this input from a text file. I am using the same numbers as aboveThe text file is this..
n=8 m=14
0
2 1
3 4
4 2
1
3 2
5 1
2
4 1
3
5 4
4
5 2
6 7
7 2
5
6 4
6
7 5
This is what I have so far which just tells me if the number is a node or an edge.
[code]
[/code]
output is node
Edge
Edge
Edge
node
Edge
Edge
node
ect..
My question is how do I do I grab this information from the text file and put it into my Edge[] ?
Explanation / Answer
//try this code...
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
class Edge
{
int x,y,z;
Edge(int x,int y,int z)
{
this.x=x;
this.y=y;
this.z=z;
}
};
public class red {
public static void main(String argv[]) throws FileNotFoundException, IOException
{
BufferedReader inputReader,ir;
Scanner lineScanner;
int countInput;
String line;
inputReader = new BufferedReader(new FileReader("/Users/jeremiahlukus/desktop/test.txt"));
inputReader.readLine();//skips the first line
int edgecount=0;
while((line = inputReader.readLine()) != null){
lineScanner = new Scanner(line);
countInput = 0;
while(lineScanner.hasNext()){
countInput++;
lineScanner.nextInt();
//System.out.printf("%d ",lineScanner.nextInt());
}
if(countInput == 1){
System.out.printf("node ");
}
else{
System.out.printf("Edge ");
edgecount++;//counting edges to create edge count...
}
//System.out.println(line);
}
inputReader.close();
Edge[] edges = new Edge[edgecount];//creating edge array
//reading file again...
//now storing to array
inputReader = new BufferedReader(new FileReader("/Users/jeremiahlukus/desktop/test.txt"));
inputReader.readLine();//skips the first line
int source=0,i=0;
while((line = inputReader.readLine()) != null){
lineScanner = new Scanner(line);
countInput = 0;
int a[]=new int[2],k=0;
while(lineScanner.hasNext()){
countInput++;
a[k++]=lineScanner.nextInt();
//System.out.printf("%d ",lineScanner.nextInt());
}
if(countInput == 1){
source=a[k];
System.out.printf("node ");
}
else{
System.out.printf("Edge ");
edges[i++]=new Edge(source,a[k-1],a[k]);//adding to array ...
}
//System.out.println(line);
}
}
}