I have been using this; A kind of one-liner:
public static String[] ReadFileToStringArray(String ReadThisFile) throws FileNotFoundException{
return (new Scanner( new File(ReadThisFile) ).useDelimiter("\\A").next()).split("[\\r\\n]+");
}
To read a file with this type of contents(i.e. with string tokens):
abcd
abbd
dbcd
But, now my file contents are something like this:
1 2 3 4
1 2 2 4
1 5 3 7
1 7 3 8
I want these values to be read as integer.
I have seen these 1, 2 and 3 questions but they do not answer to my question.
I have tried the following but failed:
public static int[][] ReadFileToMatrix(String ReadThisFile) throws FileNotFoundException{
return (new Scanner( new File(ReadThisFile) ).useDelimiter("\\A").nextInt()).split("[\\r\\n]+");
}
Error message: Cannot invoke split(String)
on the primitive type int
. I understand the message and know it's horribly wrong :)
Can anybody suggest the right way to achieve this.
P.S. With all due respect, "NO" to solutions with loops.