Hi everyone,
I'm very, very new to java, but since i'm a girl studying a master in technologies I've been assigned to create several computer codes to solve optimization problems in a course of applied optimization. My problem is, I don't even understand some of the code given to us... :/ Can you help me? The only help I have gotten is that I have to use the functions readGraph, readLine, readInteger and readString. Can you comment it and tell me what the functions do? The meaning of them is to read in a example file of the format below. This represents a network directed graph. The first row (8) is the number of nodes. The second row and down represents bows/arcs between nodes. Eg, from node 1 goes an arc to node 3. Thank you so very much, would really appreciate it!
8
1 3
1 4
1 7
1 8
3 2
3 7
4 3
5 1
5 4
6 1
6 5
7 2
7 8
8 6
private static MyDigraph readGraph(String fileName) { MyDigraph G = null; int lineNum = 0; String line = null; StringTokenizer temp; BufferedReader inFile = null; try { inFile = new BufferedReader(new FileReader(fileName)); } catch (FileNotFoundException e) { System.out.println("?? file not found"); return null; } if ((line = readLine(inFile)) == null) { System.out.println("?? file is empty"); return null; } else { lineNum++; temp = new StringTokenizer(line); try { if (temp.countTokens() != 1) { throw new IllegalArgumentException(); } int n = Integer.parseInt(temp.nextToken()); G = new MyDigraph(n); } catch (IllegalArgumentException e) { System.out.println("?? format error on line #" + lineNum); return null; } } while ((line = readLine(inFile)) != null) { lineNum++; temp = new StringTokenizer(line); try { if (temp.countTokens() != 2) { throw new IllegalArgumentException(); } int u = Integer.parseInt(temp.nextToken()); int v = Integer.parseInt(temp.nextToken()); G.insertEdge(u, v); } catch (IllegalArgumentException e) { System.out.println("?? format error on line #" + lineNum); } } return G; } private static int readInteger(String prompt) { int temp = 0; while (true) { try { temp = Integer.parseInt(readString(prompt)); break; } catch (NumberFormatException e) { System.out.println("?? format error"); } } return temp; } private static String readString(String prompt) { StringTokenizer temp = null; BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); while (true) { System.out.print(prompt); System.out.flush(); temp = new StringTokenizer(readLine(stdin)); if (temp.countTokens() == 1) break; System.out.println("?? format error"); } return temp.nextToken(); } private static String readLine(BufferedReader inFile) { String line = null; try { line = inFile.readLine(); } catch (IOException e) { System.err.println("?? beats me ..."); System.exit(0); } return line; } }