I have a text file and when it reads column one it is returning the last name on
ID: 3689853 • Letter: I
Question
I have a text file and when it reads column one it is returning the last name on the list but 5x in a row instead of the other 4 names. Please Help
{
public partial class MainForm : Form
{
//List to hold User Entry Objects
List<PersonEntry> userList = new List<PersonEntry>();
//Field to hold PersonEntry Objects for AddressBook
private List<PersonEntry> addressBook = new List<PersonEntry>();
public MainForm()
{
InitializeComponent();
}
//ReadFile method reads the Text File created and stores it into PersonEntry
private void ReadFile()
{
try
{
StreamReader inputFile;
string line;
//Create an instance of PersonEntry structure
PersonEntry entry = new PersonEntry();
//Create delimiter array
char[] delim = { ' ', '@', ',' };
////Open Address Book file
inputFile = File.OpenText("Address Book.txt");
//Read the lines from the file
while (!inputFile.EndOfStream)
{
line = inputFile.ReadLine();
//Tokenize the line
string[] tokens = line.Split(delim);
//Store tokens in entry object
entry.Name = tokens[0];
//entry.Email = tokens[1];
//entry.Phone = tokens[2];
userList.Add(entry);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//DisplayNames method displays the list of names in the contactsListBox control
private void DisplayNames()
{
foreach (PersonEntry entry in userList)
{
contactsListBox.Items.Add(entry.Name);
}
}
//The GetUserData method accepts a PersonEntry object as an argument. It assigns the data entered by the user to the object's properties.
private void GetUserData(PersonEntry info)
{
//Get the user's name, email, phone
info.Name = nameTextBox.Text;
info.Email = emailTextBox.Text;
info.Phone = phoneTextBox.Text;
}
private void addInfoButton_Click(object sender, EventArgs e)
{
//Create a PersonEntry object
PersonEntry myUserInfo = new PersonEntry();
//Get the phone data
GetUserData(myUserInfo);
//Add the PersonEntry object to the List
userList.Add(myUserInfo);
//Add an entry to the list box
contactsListBox.Items.Add(myUserInfo.Name);
//Clear the TextBox controls
nameTextBox.Clear();
emailTextBox.Clear();
phoneTextBox.Clear();
//Reset the focus
nameTextBox .Focus();
}
private void contactsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
//Get the index of the selected item
int index = contactsListBox.SelectedIndex;
//Create the PriceForm object
EmailPhoneForm myForm = new EmailPhoneForm();
myForm.UserEmail = userList[index].Email;
myForm.UserPhone = userList[index].Phone;
//Display the PriceForm to show the price
myForm.ShowDialog();
}
private void MainForm_Load(object sender, EventArgs e)
{
//Read the Address Book.txt file
ReadFile();
//Display the names
DisplayNames();
}
}
}
Explanation / Answer
// here, if each line contains 5 names
// then loop through all tokens and add every token into userList
for(int i = 0; i < tokens.Count; ++i){
PersonEntry entry = new PersonEntry();
entry.Name = tokens[i];
userList.Add(entry);
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
{
public partial class MainForm : Form
{
//List to hold User Entry Objects
List<PersonEntry> userList = new List<PersonEntry>();
//Field to hold PersonEntry Objects for AddressBook
private List<PersonEntry> addressBook = new List<PersonEntry>();
public MainForm()
{
InitializeComponent();
}
//ReadFile method reads the Text File created and stores it into PersonEntry
private void ReadFile()
{
try
{
StreamReader inputFile;
string line;
//Create an instance of PersonEntry structure
//Create delimiter array
char[] delim = { ' ', '@', ',' };
////Open Address Book file
inputFile = File.OpenText("Address Book.txt");
//Read the lines from the file
while (!inputFile.EndOfStream)
{
line = inputFile.ReadLine();
//Tokenize the line
string[] tokens = line.Split(delim);
//Store tokens in entry object
// here, if each line contains 5 names
// then loop through all tokens and add every token into userList
for(int i = 0; i < tokens.Count; ++i){
PersonEntry entry = new PersonEntry();
entry.Name = tokens[i];
//entry.Email = tokens[i + 1];
//entry.Phone = tokens[i + 2];
// i += 2
userList.Add(entry);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//DisplayNames method displays the list of names in the contactsListBox control
private void DisplayNames()
{
foreach (PersonEntry entry in userList)
{
contactsListBox.Items.Add(entry.Name);
}
}
//The GetUserData method accepts a PersonEntry object as an argument. It assigns the data entered by the user to the object's properties.
private void GetUserData(PersonEntry info)
{
//Get the user's name, email, phone
info.Name = nameTextBox.Text;
info.Email = emailTextBox.Text;
info.Phone = phoneTextBox.Text;
}
private void addInfoButton_Click(object sender, EventArgs e)
{
//Create a PersonEntry object
PersonEntry myUserInfo = new PersonEntry();
//Get the phone data
GetUserData(myUserInfo);
//Add the PersonEntry object to the List
userList.Add(myUserInfo);
//Add an entry to the list box
contactsListBox.Items.Add(myUserInfo.Name);
//Clear the TextBox controls
nameTextBox.Clear();
emailTextBox.Clear();
phoneTextBox.Clear();
//Reset the focus
nameTextBox .Focus();
}
private void contactsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
//Get the index of the selected item
int index = contactsListBox.SelectedIndex;
//Create the PriceForm object
EmailPhoneForm myForm = new EmailPhoneForm();
myForm.UserEmail = userList[index].Email;
myForm.UserPhone = userList[index].Phone;
//Display the PriceForm to show the price
myForm.ShowDialog();
}
private void MainForm_Load(object sender, EventArgs e)
{
//Read the Address Book.txt file
ReadFile();
//Display the names
DisplayNames();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.