2

I am trying to use data from text file (matrix [i, j]). I have code works with one dimension array but doesn't work with two dimension array. I tried to use different method but I always get an error.

 string fileContent = File.ReadAllText(file path);
 string[] integerStrings = fileContent.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
 integers = new int[integerStrings.Length];
 for (int n = 0; n < integerStrings.Length; n++)
 integers[n] = int.Parse(integerStrings[n]);

I modify it to this

  string fileContent = File.ReadAllText(path);
        string[,] integerStrings = fileContent.Split(new char[,] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
        integers = new int[integerStrings.Length,2];
        for (int n = 0; n < integerStrings.Length; n++)
            for (int j = 0; j <2; n++)
                integers[n,j] = int.Parse(integerStrings[n,j]);

the text file

0   0   
2   0   
4   0   
6   0   
8   1   
10  1   
12  1   
14  2   
16  3   
18  3   

Note the code I need should be fixable with rows number

9
  • there is no invalid number Commented Jun 19, 2016 at 6:24
  • What is the error you are getting? What algorithm did you use and how the numbers are represented in your text? Commented Jun 19, 2016 at 6:25
  • one second let me type the code Commented Jun 19, 2016 at 6:30
  • How does your input file looks like? Commented Jun 19, 2016 at 6:30
  • 1
    Split method does not give you 2d array.... You have to show the format of your text... An example so we can help. Commented Jun 19, 2016 at 6:32

2 Answers 2

1

get the lines from the file then split each line to get your 2d array. Here is a rough first draft. You can test and refactor to improve on it if needed.

int[,] matrix = null;
int rowCount = 0;
int colCount = 0;
var lines = File.ReadAllLines(path);
rowCount = lines.Length;
for(int i = 0; i < rowCount; i++) {
    var line = lines[i];
    var tokens = line.Split(new []{' ', '\t'}, StringSplitOptions.RemoveEmptyEntries);        
    if(matrix == null) {
        colCount = tokens.Length;
        matrix = new int[rowCount, colCount];
    }
    for(int j = 0; j < colCount; j++) {
        matrix[i, j] = int.Parse(tokens[j]);
    }
}

this part is for display the matrix

int rowLength = matrix.GetLength(0);
int colLength = matrix.Rank;

for (int i = 0; i < rowLength; i++) {
    for (int j = 0; j < colLength; j++) {
        Console.Write(string.Format("{0} ", matrix[i, j]));
    }
    Console.WriteLine();
    Console.WriteLine();
}
Console.ReadLine();
Sign up to request clarification or add additional context in comments.

9 Comments

File.ReadAllLines(path) give me an error I tried to use ReadAllTexts but will give me another error at the line split ..
still getting an error matrix[i, j] = int.Parse(tokens[j]); ` {"Input string was not in a correct format."}`
I just tested this exact code against your input from the example and got no errors. what did you change from what I provided? This works for int only based on your question so make sure that that you don't have any floating point numbers in your input
this is link to google drive you can download the txt file drive.google.com/file/d/0BypKQy6gqhpkLUthYjhoaE84YWM/…
You sample file is separated with tabs. Made another edit to cater for the tab.
|
1

If you want 2d array split method just gives 1d array so you have to split twice...

First split by line breaks then by spaces...

string[] rows = fileContent.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

int[,] result = new int[rows.Length,2];

for(int i = 0; i < rows.Length; i++)
{
     var col = rows[i].Split(new char{' ','\t'}, StringSplitOptions.RemoveEmptyEntries);

     result[i,0] = int.Parse(col[0]);
     result[i,1] = int.Parse(col[1]);
}

3 Comments

col would be a 1d array resulting from Split. But you are using it as a 2d array in the following lines.
@Nkosi thanks for that. Im posting with my phone and its hard to do without keyboard! ;)
still getting an error {"Input string was not in a correct format."}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.