java - Reading each Integer from a line from a Text file -
i new java. how can read each integer line in text file. know reader class has read , readline functions it. in case not want read integers in file want know when line changes. because first element of every line denotes array index , corresponding elements linked list values attached index.
for example, see 4 line below. in case not want read each integer first integer of every line array index have 4 element array each array element correspoding list a[1]-> 4, a[2]-> 1,3,4 , soo on.
1 4
2 1 3 4
3 2 5
4 2
after retrieving integers planning populate them via
arraylist<integer>[] alist = (arraylist<integer>[]) new arraylist[numlines];
edited : had been asked in 1 comments have thinked soo far , exctly stucken below thinking (in terms of original , pseoudo code mixed)..
while (lr.readline() != null) { while ( // loop through each character) if ( first charcter) alist[index] = first character; else alist[index]->add(second char.... last char of line); }
thanks
thanks scanner hint, andrew thompson.
this how have achieved
scanner sc =new scanner(new file("filename.txt")); arraylist<integer>[] alist = (arraylist<integer>[]) new arraylist[200]; string line; sc.usedelimiter("\\n"); int vertex = 0; while (sc.hasnextline()) { int edge = 0; line = sc.nextline(); scanner linesc = new scanner(line); linesc.usedelimiter("\\s"); vertex = linesc.nextint() - 1; alist[vertex] = new arraylist<integer>(); int tmp = 0; system.out.println(vertex); while (linesc.hasnextint()) { edge = linesc.nextint(); alist[vertex].add(edge); system.out.print(alist[vertex].get(tmp) + " "); ++tmp; } system.out.println (); }
Comments
Post a Comment