Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Reading from file

  1. Sep 22, 2011 #1
    hi want to read the rows of a matrix from a file which are written in the following way :

    (2:1,3), (4:3,1), (2:1,8)

    i am working in java, i am not able to figure out how to do this using bufferedreader :(

    can someone help?

  2. jcsd
  3. Sep 23, 2011 #2


    User Avatar
    Science Advisor

    What is the exact meaning of the syntax (2:1,3) etc?
  4. Sep 23, 2011 #3
    (2:1,3) represents 2,1 and 3 elements of a row. ":" and "," are just the delimiters, my task is to separate the integers from these delimiters
  5. Sep 23, 2011 #4


    User Avatar
    Science Advisor

    Ok got it. The row element delimiter can be either "," or ':". They are equivalent and they have no other special meaning.

    Just to confirm then, in your example you're inputting a 3x3 matrix like the matlab equiv of [2,3,1; 4,3,1; 2,1,8]

    Code (Text):

    2 3 1
    4 3 1
    2 1 8
    I'm no expert on java, but if you cant find any easier method then you could always use "bufferedreader.readln()" to read the entire line as a string and then just go through it character by character. Parsing from first principles as it were.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook