Java Reference
In-Depth Information
1 /**
2 * A main routine that:
3 * 1. Reads a file (supplied as a command-line parameter)
4 * containing edges.
5 * 2. Forms the graph;
6 * 3. Repeatedly prompts for two vertices and
7 * runs the shortest path algorithm.
8 * The data file is a sequence of lines of the format
9 * source destination cost
10 */
11 public static void main( String [ ] args )
12 {
13 Graph g = new Graph( );
14 try
15 {
16 FileReader fin = new FileReader( args[0] );
17 Scanner graphFile = new Scanner( fin );
18
19 // Read the edges and insert
20 String line;
21 while( graphFile.hasNextLine( ) )
22 {
23 line = graphFile.nextLine( );
24 StringTokenizer st = new StringTokenizer( line );
25
26 try
27 {
28 if( st.countTokens( ) != 3 )
29 {
30 System.err.println( "Skipping bad line " + line );
31 continue;
32 }
33 String source = st.nextToken( );
34 String dest = st.nextToken( );
35 int cost = Integer.parseInt( st.nextToken( ) );
36 g.addEdge( source, dest, cost );
37 }
38 catch( NumberFormatException e )
39 { System.err.println( "Skipping bad line " + line ); }
40 }
41 }
42 catch( IOException e )
43 { System.err.println( e ); }
44
45 System.out.println( "File read..." );
46
47 Scanner in = new Scanner( System.in );
48 while( processRequest( in, g ) )
49 ;
50 }
figure 14.14
A simple main
Search WWH ::




Custom Search