Monday 15 June 2015

java - creating 2 dimensional array from text file -



java - creating 2 dimensional array from text file -

i trying add together values 2d array text file. cannot figure out how convert input file teacher's array format. file contains first line goes array represents vertices. part works fine. every line after first line contains numbers adjacency matrix. these numbers read in reason slots in matrix not beingness filled. need utilize numbers fill double array same using statement:

int[][] edges = { {0,1}, {0,2}, {1,0},{1,2}, {2,0},{2,1},{2,3},{2,4},{2,6}, {3,2},{3,4},{3,5}, {4,2},{4,3}, {5,3},{5,6}, {6,2},{6,5},{6,7}, {7,6}

i have read input txt file. names represent names of nodes or vertices in graph. numbers represent positions of vertices in adjacency matrix. illustration first line represents row o maps node john, , means john connected border node 1 , 2 peter , mary. numbers should map same way array creation above. here file looks :

john;peter;mary;susan;george;debbie;tom;bill; 0 1 2 1 0 2 2 0 1 3 4 6 3 2 4 5 4 2 3 5 3 6 6 2 5 7 7 6

and here code :

import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class testsocialgraph { public static void main(string args[]){ string verts; int[][] edges; int[] intpasser; file input = new file("graphinput.txt"); string[] names; seek { scanner reader = new scanner(input); verts = reader.next(); names = verts.split(";"); edges = new int[names.length][names.length]; while (reader.hasnextline()){ int b = 0; int count = 0; string passer = reader.nextline(); // system.out.println(passer); string[] temp = passer.split(" "); intpasser = new int[temp.length]; for(int = 1; i< temp.length; i++){ system.out.println(temp[i]); intpasser[b] = integer.parseint(temp[i]); b++; } system.out.println(intpasser[0]); for(int = 0; i< intpasser.length; i++){ edges[count][intpasser[i]] = 1; } count++; } } grab (filenotfoundexception e) { system.out.println("file not found. please place appropriate file , restart program"); system.exit(1); } } }

it looks you're trying create adjacency matrix...

i.e. first line in file says

0 1 2

so, mean node 0 connected node 1 & node 2. matches teacher has given also:

{0,1}, {0,2},

so, in final 2d array edges, should represented (1s in indexes node 0 connected to):

edges[0] = {0, 1, 1, 0, 0, 0, 0, 0};

dici's response absolutely right in matter - it's count variable's location creating problem. place int count = 0; outside while loop, , utilize readline() instead of read(), , should adjacency matrix representation of data.

the problem teacher's representation & 1 you're trying produce here different. so, consequently, later logic processes these inputs vary. 1 programme run teacher has provided, won't run matrices you've created.

---------------------------edit---------------------------

in response comment, imo, you'll need go through file twice - first number of edges (in case 20), fill edges array actual values.

import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class testsocialgraph { public static void main(string args[]) { int[][] edges; file input = new file("graphinput.txt"); seek { scanner reader = new scanner(input); reader.nextline();// skips first line (namelist) int count = 0; // count total number of 2 element arrays while (reader.hasnextline()) { string passer = reader.nextline(); count += passer.split(" ").length - 1; } reader.close(); edges = new int[count][]; int = 0; reader = new scanner(input); reader.nextline();// skips first line (namelist) while (reader.hasnextline()) { string passer = reader.nextline(); string[] split = passer.split(" "); (int j = 1; j < split.length; j++) { edges[i + j - 1] = new int[2]; edges[i + j - 1][0] = integer.parseint(split[0]); edges[i + j - 1][1] = integer.parseint(split[j]); } += split.length - 1; } reader.close(); print(edges); } grab (filenotfoundexception e) { system.out.println(e); } } private static void print(int[][] e) { (int = 0; < e.length; i++) { (int j = 0; j < e[i].length; j++) system.out.print(e[i][j] + " "); system.out.println(); } } }

i hope code isn't obscure/unreadable.

java arrays file-io

No comments:

Post a Comment