0

I am getting an error when running my program. It says system.IndexOutOfRangeException'Index was out of bounds of array'. How can I solve this? I've been stuck for hours nowIn your C# solution, develop a project and associated classes that will be dedicated to reading in the data from the input flat text file (../10) as well as writing data out to an output flat text file in the exact format as in the given sample output file (../

    static void Main(string[] args)
    {


        string filePath = @"C:\\Users\Mthobeli M\Downloads\airbnb_listings_cpt2.input";
        string filePath1 = @"C:\\Users\Mthobeli M\Downloads\airbnb_listings_cpt.output";

        List<Info> info = new List<Info>();
        List<string> lines = File.ReadAllLines(filePath).ToList();


        Console.WriteLine("AirBNB Quick Search");

        foreach (string line in lines)
        {
            string[] entries = line.Split(';');

            Info newInfo = new Info();

            newInfo.id = entries[0];
            newInfo.host_ID = entries[1];
            newInfo.host_Name = entries[2];
            newInfo.host_since = entries[3];
            newInfo.host_location = entries[4];
            newInfo.host_response_time = entries[5];
            newInfo.host_response_rate = entries[6];
            newInfo.host_is_superhost = entries[7];
            newInfo.host_listings_count = entries[8];
            newInfo.host_total_listings_count = entries[9];
            newInfo.host_identity_verified = entries[10];
            newInfo.street = entries[11];
            newInfo.city = entries[12];
            newInfo.zipcode = entries[13];
            newInfo.market = entries[14];
            newInfo.country_code = entries[15];
            newInfo.country = entries[16];
            newInfo.latitude = entries[17];
            newInfo.longitude = entries[18];
            newInfo.property_type = entries[19];
            newInfo.room_type = entries[20];
            newInfo.accommodates = entries[21];
            newInfo.bathrooms = entries[22];
            newInfo.bedrooms = entries[23];
            newInfo.beds = entries[24];
            newInfo.square_feet = entries[25];
            newInfo.price = entries[26];
            newInfo.weekly_price = entries[27];
            newInfo.monthly_price = entries[28];
            newInfo.security_deposit = entries[29];
            newInfo.cleaning_fee = entries[30];
            newInfo.guests_included = entries[31];
            newInfo.extra_people = entries[32];
            newInfo.minimum_nights = entries[33];
            newInfo.maximum_nights = entries[34];
            newInfo.review_scores_cleanliness = entries[35];
            newInfo.review_scores_checkin = entries[36];
            newInfo.review_scores_communication = entries[37];
            newInfo.review_scores_location = entries[38];
            newInfo.review_scores_value = entries[39];

            info.Add(newInfo);

        }
        foreach (var note in info)
        {
            Console.WriteLine($"{ note.host_ID }; { note.host_Name }; { note.host_response_rate }; {note.host_is_superhost}; { note.host_identity_verified}; { note.property_type }; { note.accommodates }; { note.price}; { note.review_scores_cleanliness }");
        }
        List<string> output = new List<string>();


        foreach (var note in info)
        {
            output.Add($"{ note.host_ID }; { note.host_Name }; { note.host_response_rate }; {note.host_is_superhost}; { note.host_identity_verified}; { note.property_type }; { note.accommodates }; { note.price}; { note.review_scores_cleanliness }");
        }


        File.WriteAllLines(filePath1, output);

        Console.ReadLine();
    }

}

}

  • Probably there is a line that has not 40 entries. You don't check the length of the entries array and of course this could end with that message with a line with fewer fields – Steve Jun 24 '20 at 15:03
  • comment out the entire part where you initialize `newInfo` and replace it with: `if(entries.Length < 40)Console.WriteLine($"Line: {line} Length: {entries.Length}");` and let it run. Preferably use a normal for loop then you even would have the line number and you could jump right to it in your editor. Actually when you run it in debug mode in visual studio, the compiler should by default halt when the exception is thrown. This gives you the possibility to examine the variables. – Mong Zhu Jun 24 '20 at 15:06
  • 1
    do not parse what is obviously a CSV file yourself. [there are libraries that do the same thing, only better](https://joshclose.github.io/CsvHelper/) – Franz Gleichmann Jun 24 '20 at 15:06

0 Answers0