arrays - separate string input from text file java -
i'm java beginner , have small question scanning text file suppose have text file this
abc 012 4g5 (0 0 0) (0 1 3) (1 2 6) (1 1 0) abcde blahblah
now want make array string inside parenthesis, meaning how tell scanner scan strings started first open parentheses, reset array input after following right parentheses, , stop scanning after last right parentheses. have far:
*for array, take first digit row#, second digit col# , third digit value
while (file.hasnext()) { if (file.next().equals("(")) { { 2darray[integer.parseint(file.next())][integer.parseint(file.next())] = file.next(); } while (!file.next().equals(")")); }
thanks
i recommend used regex match parameters.
have mention file in below case bufferedreader
. document on that.
while ((line = file.readline()) != null) { if( line.matches("\\((.*?)\\)") ) // match string between 2 parantheses { string trimmedline = line.substring(1, line.length - 1); // takes string without parantheses string[] result = trimmedline.split(" "); // split @ white space } } // result[0] row# // result[1] col# // result[2] value
a flaw in code must respect text line formatting mentioned in question (e.g. "(3 5 6)"
).
Comments
Post a Comment