Java Scanner delimiter on values of nextLine -
i have file in .dat format. here sample
||= (n ) =|| 1|| 0.938 || --- || 0.5 || (****)|| 0.5 || 0 || 0 || 0 || 0.700 || (p)=2212, (n)=2112 ||
||= (\delta ) =|| 2|| 1.232 || 0.118 || 1.5 || (****)|| 1.5 || 0 || 0 || 3 || 1.076 || (\delta^{++})=2224, (\delta^+)=2214, (\delta^0)=2114, (\delta^-)=1114 ||
||= (p_{11}(1440) ) =|| 3|| 1.462 || 0.391 || 0.5 || (****)|| 0.5 || 0 || 0 || 3 || 1.076 || 202212, 202112 ||
||= (s_{11}(1535) ) =|| 4|| 1.534 || 0.151 || 0.5 || ( ***)|| 0.5 || 0 || 0 || 3 || 1.076 || 102212, 102112 ||
i trying use scanner read file , delimit line "||" , send , arraylist future processing. here sample of code use delimiter
string file = "data.dat"; scanner s = null; try { s = new scanner(new file(file)).usedelimiter("\\|\\|"); //here use of delimiter arraylist<string> list = new arraylist<string>(); while (s.hasnextline()) { //notice using hasnextline because each line must unique create hashmap list.add(s.nextline()); } s.close(); (string string : list) { //lets print out values of list system.out.println(string); } } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); }
but output still has values of delimiter, i.e. here output:
||= (n ) =|| 1|| 0.938 || --- || 0.5 || (****)|| 0.5 || 0 || 0 || 0 || 0.700 || (p)=2212, (n)=2112 ||
||= (\delta ) =|| 2|| 1.232 || 0.118 || 1.5 || (****)|| 1.5 || 0 || 0 || 3 || 1.076 || (\delta^{++})=2224, (\delta^+)=2214, (\delta^0)=2114, (\delta^-)=1114 ||
||= (p_{11}(1440) ) =|| 3|| 1.462 || 0.391 || 0.5 || (****)|| 0.5 || 0 || 0 || 3 || 1.076 || 202212, 202112 ||
||= (s_{11}(1535) ) =|| 4|| 1.534 || 0.151 || 0.5 || ( ***)|| 0.5 || 0 || 0 || 3 || 1.076 || 102212, 102112 ||
i have searched , found no answer helped. seeing warning "resource leak: '' never closed" line
s = new scanner(new file(file)).usedelimiter("\\|\\|");
which disappears if line broken into
s = new scanner(new file(file)); s.usedelimiter("\\|\\|");
any appreciated.
you don't need use backslash escape. instead of \\|\\|
delimiter, split string after it's read file.
string file = "data.dat"; scanner s = null; try { s = new scanner(new file(file)); //no more delimiter. it's not needed arraylist<string> list = new arraylist<string>(); while (s.hasnextline()) { string[] strings = s.nextline().split("[||]"); (int = 0; < strings.length; i++) { list.add(strings[i]); } } s.close(); (string string : list) { //lets print out values of list system.out.println(string); } } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); }
this should fix it.
Comments
Post a Comment