I have an matrix in this format that I am trying to validate and remove first row:
3 4
0 0 0
0 0 0
0 0 0
0 0 0
Where the first line is and the other lines are the actual data.
Width Height
What is the best way to A remove the first row, and B validate that all rows meet the Width Height Criteria specified? I could do a simple for loop and copy them but I am looking for a more elegant way to do it? Maybe with Linq or one of the Collection Methods?
So far I have:
//add the split for correctness
string[][] lines = File.ReadAllLines(fileName).Select(x=>x.Split(' ')).ToArray();
//first line is width/hight
int length = lines.Length ==0 ;
if(|| (length > 0 && lines[0].Length !=2 ) ){
throw new InvalidDataException("File is not correctly formated:" + fileName);
}
int width = lines[0][0];
int hieght = lines[0][1];
//Check Row count
if(length != height -1){
throw new InvalidDataException("Invalid missing rows in the Matrix definition");
}
//make sure the file is correctly formated with width and height:
if(lines.Any(x=>x.Length != Width)){
//I know this fails because of first line
throw new InvalidDataException("Invalid Width in a row in the Matrix");
}
Any suggestions on a better way to validate input?