java - How to to add values from a file into an array using split? -


i have following code:

bufferedreader metaread = new bufferedreader(new filereader(metafile)); string metaline = ""; string [] metadata = new string [100000];  while ((metaline = metaread.readline()) != null){     metadata = metaline.split(",");     (int = 0; < metadata.length; i++)         system.out.println(metadata[0]); } 

this what's in file:

testtable2  name    java.lang.integer   true    test testtable2  age     java.lang.string    false   test testtable2  id      java.lang.integer   false   test 

i want array have @ metadata[0] testtable2, metadata[1] name, when run @ 0 testtable2testtable2testtable2, , @ 1 i'd nameageid , outofboundsexception.

any ideas in order result want?

just print metadata[i] instead of metadata[0] , split each string "[ ]+" (that means "1 or more spaces"):

metadata = metaline.split("[ ]+"); 

as result, following arrays:

[testtable2, name, java.lang.integer, true, test] [testtable2, age, java.lang.string, false, test] [testtable2, id, java.lang.integer, false, test] 

the code snippet preceding output results:

while ((metaline = metaread.readline()) != null) {     metadata = metaline.split("[ ]+");     (int = 0; < metadata.length; i++)         system.out.print(metadata[i] + " ");     system.out.println(); } 

also, i've written task using java 8 , stream api:

list<string> collect = metaread             .lines()             .flatmap(line -> arrays.stream(line.split("[ ]+")))             .collect(collectors.tolist()); 

and, finally, there straight-forward way:

final int lines, words; string[] metadata = new string[lines = 5 * (words = 3)]; // don't  int = 0; while ((metaline = metaread.readline()) != null) {     (string s : metaline.split("[ ]+")) metadata[i++] = s; } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -