Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using following code to read the data from log files and when lineWords[2].EndsWith("x"),I want to save lineWords[0] to string[] words and I want return both lineWords[0] and lineWords[2] when the method is called


C#
public static string[] words(string file)
{
                 string[] fileContents = null;
                 string[] words = null;
                
                 try
                 {
                     // Open and read the entire Data Log File contents.
                     fileContents = File.ReadAllLines(file);
                 }
                 catch (Exception ex)
                 {
                     throw new Exception("The Input log file was not found or is not in correct format\nDetails: " + ex.Message);
                 }

                 if (fileContents == null)
                     throw new Exception("The Input log file was not found or is not in correct format");

                 // Process each line of the Data Log File and filter out the CAN Msg Id's corresponding to each CAN Bus.
                 for (int Index = 0; Index < fileContents.Length; Index++)
                 {
                     string CANMsgId = string.Empty;
                     string[] spaceSeperator = new string[] { " " };
                     string[] lineWords = (fileContents[Index].Trim()).Split(spaceSeperator, StringSplitOptions.RemoveEmptyEntries);
                     if (lineWords.Length < (2 + 1))

                         continue;

                     // If a CAN Msg Id is valid, it should end with 'x'. If it doesnot end with 'x',
                     // then skip the entry and go to the next line of log file
                     if (lineWords[2].EndsWith("x"))
                     {
                         int i = 1;
                         CANMsgId = lineWords[2].TrimEnd('x');
                        words[i] = lineWords[0];
                        i++;
                     }
                     
                 }


           
           return words;
                
     
} 


I am trying some thing like above, but I am getting some exception at this line
C#
words[i] = lineWords[0];


Can anyone help me in solving this


Thanks
John
Posted

You should use a list instead of an array:

C#
public static string[] words(string file)
{
                 string[] fileContents = null;
                 List<string> words = new List<string>();
                
                 try
                 {
                     // Open and read the entire Data Log File contents.
                     fileContents = File.ReadAllLines(file);
                 }
                 catch (Exception ex)
                 {
                     throw new Exception("The Input log file was not found or is not in correct format\nDetails: " + ex.Message);
                 }
 
                 if (fileContents == null)
                     throw new Exception("The Input log file was not found or is not in correct format");
 
                 // Process each line of the Data Log File and filter out the CAN Msg Id's corresponding to each CAN Bus.
                 for (int Index = 0; Index < fileContents.Length; Index++)
                 {
                     string CANMsgId = string.Empty;
                     string[] spaceSeperator = new string[] { " " };
                     string[] lineWords = (fileContents[Index].Trim()).Split(spaceSeperator, StringSplitOptions.RemoveEmptyEntries);
                     if (lineWords.Length < (2 + 1))
 
                         continue;
 
                     // If a CAN Msg Id is valid, it should end with 'x'. If it doesnot end with 'x',
                     // then skip the entry and go to the next line of log file
                     if (lineWords[2].EndsWith("x"))
                     {
                         CANMsgId = lineWords[2].TrimEnd('x');
                         words.Add(lineWords[0]);
                     }
                     
                 }
 

           
           return words.ToArray();
                
     
}


This is because words array is null and you never create it. Since you don't know how many entries you will need in the array, its better to use a list. In the code you posted, you were always setting the second element (index 1) to the value, then incrementing i, and resetting it on the next loop, this does, effectively, nothing even if you had initialized the array.
 
Share this answer
 
v2
Comments
Pheonyx 18-Dec-13 10:58am    
+5 right back at you :-)
Well lets start with what exception are you getting? My guess would be a null exception because looking at your code you never instantiate "words"
Personally for the task you are doing, I wouldn't declare words as a string[].

I would declare words as :
C#
List<string> words = new List<string>();</string></string>

Then instead of words[i] = I would do:
C#
words.Add(lineWords[0]);

Finally if you have to return an array do:
C#
return words.ToArray();

Oh, and I would change:
if (lineWords.Length < (2 + 1))
to
if (lineWords.Length < 3)
 
Share this answer
 
Comments
Ron Beyer 18-Dec-13 10:42am    
+5 for coming to the same conclusion as I did :)
When you are not sure how many elements are there and if there are cases that there can be deletions, then you should always go for a LIST.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900