Java Reading Matrix from File in Java - Can Someone Help?

  • Thread starter Thread starter russel.arnold
  • Start date Start date
  • Tags Tags
    File Reading
Click For Summary
The discussion focuses on reading matrix rows from a file in Java, specifically formatted as (2:1,3), (4:3,1), (2:1,8). The syntax indicates that the numbers are separated by delimiters ":" and ",", which serve to delineate the integers within the matrix. The input represents a 3x3 matrix similar to MATLAB's format. A suggested approach for reading the data involves using BufferedReader to read the entire line as a string and then parsing it character by character to extract the integers. This method is recommended for those who may not have access to simpler parsing techniques.
russel.arnold
Messages
38
Reaction score
0
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?

Thanks
 
Technology news on Phys.org
What is the exact meaning of the syntax (2:1,3) etc?
 
(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
 
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:
2 3 1
4 3 1
2 1 8

I'm no expert on java, but if you can't 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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 57 ·
2
Replies
57
Views
5K
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K