Reading Matrix from File in Java - Can Someone Help?

  • Context: Java 
  • Thread starter Thread starter russel.arnold
  • Start date Start date
  • Tags Tags
    File Reading
Click For Summary

Discussion Overview

The discussion revolves around reading a matrix from a file in Java, specifically focusing on the format of the matrix data and how to parse it using BufferedReader. The conversation includes technical challenges related to string manipulation and data extraction.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant seeks assistance with reading matrix data formatted as (2:1,3), (4:3,1), (2:1,8) from a file using Java.
  • Another participant questions the meaning of the syntax used in the matrix representation.
  • A clarification is provided that the syntax (2:1,3) indicates the elements of a row, with ":" and "," serving as delimiters for separating integers.
  • A further response confirms the interpretation of the input as a 3x3 matrix, likening it to MATLAB syntax, and suggests using BufferedReader to read the line as a string for parsing.

Areas of Agreement / Disagreement

Participants generally agree on the interpretation of the matrix format and the approach to reading the data, but there is no consensus on the best method for parsing the data in Java.

Contextual Notes

Participants have not fully explored the implications of different parsing methods or the potential complexities involved in handling various input formats.

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.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 6 ·
Replies
6
Views
4K
  • · 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
6K
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K