0

Hi

Suppose you want to read from a file in a loop with strings of various lengths into a single string variable e.g if your file contents are such as

Steve West , 14/10/1985, Indiana, FARh+ , 01234567890, Sht. Mehmet Emin Street No:1 Gocmenkoy Nicosia KKTC

Marcus Chaplin, 13/11/1978 ,Colorado , MBRh+ , 01234567890, Kelebek Street No:11 Taskinkoy Nicosia KKTC

Here's what i've tried. Seems correct but I'm getting an exception "Array inces is out of bounds" on the line "date = words[1]" during the second loop. How can i fix this? Thanks

while (true)

            {

                info = myReader.ReadLine();

                if (info == null) break;

                string[] words = info.Split(',');

                name  = words[0];

                date  = words[1];

                place = words[2];

                blood = words[3];

                num   = words[4];

                address = words[5];
}
2
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by Momerath
0

It means you have a line that doesn't have a ',' character in it. Most likely it is the last line of the file. Try checking for an empty line before you split it.

info = myReader.ReadLine();
if (info == null) break;
if (info.Trim() != String.Empty) {
    string[] words = info.Split(',');
    if (words.Length == 6) {
        name = words[0].Trim();
        date = words[1].Trim();
        ... etc. ...
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.