G. B
2018-06-03 16:00:00 UTC
Why is this program giving me this error when you run it?
Joe,Blog,www.joeblog.com
Joe,Smith,www.joesmith.com
Sue,Blog,www.sueblog.com
<========= The program starts here =========>
Unhandled Exception: System.IndexOutOfRangeException: Index was
outside the bounds of the array.
at Program.Main()
The text file is very simple like like so:outside the bounds of the array.
at Program.Main()
Joe,Blog,www.joeblog.com
Joe,Smith,www.joesmith.com
Sue,Blog,www.sueblog.com
<========= The program starts here =========>
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main()
{
// // string[] lines = File.ReadAllLines (filePath);
// List<string> lines = File.ReadAllLines (filePath).ToList();
// foreach (string line in lines)
// {
// Console.WriteLine( line );
// }
// lines.Add("Sue,Storm,www.stormy.com");
// File.WriteAllLines (filePath, lines);
List<Person> people = new List<Person>();
List<string> lines = File.ReadAllLines(filePath).ToList();
foreach (var line in lines)
{
string[] entries = line.Split(',');
Person newPerson = new Person();
newPerson.FirstName = entries[0];
newPerson.LastName = entries[1];
newPerson.Url = entries[2];
people.Add(newPerson);
}
Console.WriteLine("Read from text file\n");
foreach (var person in people)
{
{person.Url}");
}
people.Add(new Person { FirstName = "Greg", LastName =
"Jones", Url = "www.gregjones.com" });
List<string> output = new List<string>();
foreach (var person in people)
{
output.Add($"{person.FirstName},{person.LastName},{person.Url}");
}
Console.WriteLine("Writing to text file ...\n");
File.WriteAllLines(filePath, output);
Console.WriteLine("All entries written ...\n");
Console.ReadKey();
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Url { get; set; }
}
}
<========= The program ends here =========>using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main()
{
// // string[] lines = File.ReadAllLines (filePath);
// List<string> lines = File.ReadAllLines (filePath).ToList();
// foreach (string line in lines)
// {
// Console.WriteLine( line );
// }
// lines.Add("Sue,Storm,www.stormy.com");
// File.WriteAllLines (filePath, lines);
List<Person> people = new List<Person>();
List<string> lines = File.ReadAllLines(filePath).ToList();
foreach (var line in lines)
{
string[] entries = line.Split(',');
Person newPerson = new Person();
newPerson.FirstName = entries[0];
newPerson.LastName = entries[1];
newPerson.Url = entries[2];
people.Add(newPerson);
}
Console.WriteLine("Read from text file\n");
foreach (var person in people)
{
{person.Url}");
}
people.Add(new Person { FirstName = "Greg", LastName =
"Jones", Url = "www.gregjones.com" });
List<string> output = new List<string>();
foreach (var person in people)
{
output.Add($"{person.FirstName},{person.LastName},{person.Url}");
}
Console.WriteLine("Writing to text file ...\n");
File.WriteAllLines(filePath, output);
Console.WriteLine("All entries written ...\n");
Console.ReadKey();
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Url { get; set; }
}
}