Preventing Duplicate values in a list.

comparing a number just entered to other numbers in a List, to prevent

Page 1 of 1

5 Replies - 1194 Views - Last Post: 10 October 2008 - 07:52 AM Rate Topic: -----

#1 Onker   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 15-July 08

Preventing Duplicate values in a list.

Posted 09 October 2008 - 05:29 PM

Hi guys.
As it says in the description, I'm trying to create a list, it'll have first name, last name, and an ID number. The intent is to prevent the duplication of an ID number before it gets added, and thus allow it to be corrected.

Here's my code so far, everything else works pretty well, I'm just having an issue with this step right now.

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace RonT_2
{
	class RonT
	{
		public struct Info
		{
			public string firstName;
			public string lastName;
			public int idNum;

			public Info(string firstName, string lastName, int idNum)
			{
				this.firstName = firstName;
				this.lastName = lastName;
				this.idNum = idNum;
			}
		}

		static void Main(string[] args)
		{
			List<Info> students = new List<Info>();

			bool again = true;
			Console.Clear();

			Console.WriteLine("Please input your student information: ");

			while (again)
			{
				Console.Write("\nFirst Name: ");
				string firstName = Console.ReadLine();
				Console.Write("Last Name: ");
				string lastName = Console.ReadLine();
				Console.Write("Id: ");
				string input3 = Console.ReadLine();
				int idNum = Convert.ToInt32(input3);


				
				Console.WriteLine("\n\nMore Students? (y/n)");
				char input4 = Console.ReadKey().KeyChar;

				if (input4 == 'n')
					again = false;
				else if (input4 == 'y')
					again = true;
				else
				{
					Console.WriteLine("You have not entered a valid response. Goodbye.");
					again = false;
				}
			}

			bool Run = true;
			while (Run)
			{
				Console.Clear();
				Console.WriteLine("Select a method to sort and display the students");
				Console.WriteLine("1. By first name");
				Console.WriteLine("2. By last name");
				Console.WriteLine("3. By student ID number");
				Console.WriteLine("4. All of the above");
				Console.WriteLine("5. Exit");
				char Selection = Console.ReadKey().KeyChar;

				switch (Selection)
				{
					case '1': students.Sort(new SortByFirstName());
						Console.WriteLine("Student information sorted with first name");
						printStudents(students);
						Console.ReadKey();
						break;
					case '2': students.Sort(new SortByLastName());
						Console.WriteLine("Student information sorted with last name");
						printStudents(students);
						Console.ReadKey();
						break;
					case '3': students.Sort(new SortByID());
						Console.WriteLine("Student information sorted with ID number");
						printStudents(students);

						Console.ReadKey();
						break;
					case '4': students.Sort(new SortByFirstName());
						Console.WriteLine("Student information sorted with first name");
						printStudents(students);

						students.Sort(new SortByLastName());
						Console.WriteLine("Student information sorted with last name");
						printStudents(students);

						students.Sort(new SortByID());
						Console.WriteLine("Student information sorted with ID number");
						printStudents(students);

						Console.ReadKey();
						break;
					case '5': Run = false;
						break;
				}
			}
			Console.ReadKey();

	}
	static void printStudents(List<Info> lst)
	{
		foreach (Info s in lst)
		Console.WriteLine("{0:000000000}\t{1}, {2}", s.idNum, s.lastName, s.firstName);
		Console.WriteLine();
	}
	
	class SortByID : IComparer<Info>
	{
		public int Compare(Info a, Info b)
		{
			return a.idNum.CompareTo(b.idNum);
		}
	}
	
	class SortByLastName : IComparer<Info>
	{
		public int Compare(Info a, Info b)
		{
			return a.lastName.CompareTo(b.lastName);
		}
	}
	
	class SortByFirstName : IComparer<Info>
	{
		public int Compare(Info a, Info b)
		{
			return a.firstName.CompareTo(b.firstName);
		}
	}

	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Preventing Duplicate values in a list.

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Preventing Duplicate values in a list.

Posted 09 October 2008 - 06:47 PM

You are not saving the student information anywhere. Below I have added a new
student info struct. Each input string is copied to the appropriate struct field.
Before the new student struct is added to the students list, the ID number is
compared against the existing ID numbers. If it already exists, the new record is
NOT added to the students list.

#region one
			while (again)
            {
                bool dupIdNum = false;

                Info newStudent = new Info();

				Console.Write("\nFirst Name: ");
				string firstName = Console.ReadLine();

                newStudent.firstName = firstName;

				Console.Write("Last Name: ");
				string lastName = Console.ReadLine();

                newStudent.lastName = lastName;

				Console.Write("Id: ");
				string input3 = Console.ReadLine();
				int idNum = Convert.ToInt32(input3);

                newStudent.idNum = idNum;

                foreach (Info st in students)
                {
                    if (st.idNum == newStudent.idNum)
                    {
                        dupIdNum = true;

                        Console.WriteLine("Id number already in use:");
                        Console.WriteLine(st.firstName);
                        Console.WriteLine(st.lastName);
                        Console.WriteLine(st.idNum);
                        Console.WriteLine("Hit any key to continue");
                        Console.ReadKey();
                    }
                }

                if (dupIdNum == false)
                    students.Add(newStudent);

                Console.WriteLine("\n\nMore Students? (y/n)");
				char input4 = Console.ReadKey().KeyChar;

				if (input4 == 'n')
					again = false;
                else if (input4 == 'y')
                    again = true;
                else
                {
                    Console.WriteLine("You have not entered a valid response. Goodbye.");
                    again = false;
                }
            }
            #endregion



HTH :)

Jeff
Was This Post Helpful? 1
  • +
  • -

#3 Onker   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 15-July 08

Re: Preventing Duplicate values in a list.

Posted 09 October 2008 - 06:54 PM

Very nice, thank you very much Jeff. I knew I'd started making it more of a mess than it had to be.

A few more things to work out, hopefully I'll be ok now though. If not, I'll post again soon.
Was This Post Helpful? 0
  • +
  • -

#4 Onker   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 15-July 08

Re: Preventing Duplicate values in a list.

Posted 09 October 2008 - 08:03 PM

Well, I'm down to the last parts now. I've got about 6 things left to build but there are three things that're making me scratch my head. I'd have some coffee and keep at it, if I didn't have to get some sleep for work and all.

Anywho, less chat more details I suppose.

Things left to do:

#1 - Figure out Why it is when I input an ID code on the second student, if I use the same ID code, it doesn't check against the students List. I can't figure it out, although some of that could be the time.

#2 - add fields to the struct Info to give myself the ability to input something like this...

Quote:
Please input your student information:

KSU Id: 1234
First Name: Mary
Last Name: Jane
Midterm: 90
Final: 80
homework: 100
homework: 80
homework: 70
homework: 80
homework:

This is the first time I've ever had to deal with "Struct" and none of my reference materials tells me how malleable it is. Everything seems to indicate it's very simplistic...so do I have the ability to continually populate homework amounts? I can see the way the professor has it set up hitting enter with nothing on the homework line ends the cycle.


#3 - use those numbers to create a grade for the student, Midterm + Final * .4 + totalHomework / number homeworks * .2. I can do the math on that one easy enough..it's just figuring out how to pull out the homework numbers. Well, I could probably figure out how to pull them out, if I could figure out how to put them in.

Beyond that...everything else is gravy and easy. I'm just stumped on those three parts. If anyone can spare me some help, that'd be awesome and appreciated.

Here's the modified code to this point.

public struct Info
		{
			public string firstName;
			public string lastName;
			public int idNum;
			//public int Midterm;
			//public int Final;

		   

			public Info(string firstName, string lastName, int idNum)
			{
				this.firstName = firstName;
				this.lastName = lastName;
				this.idNum = idNum;
			}
		}

		static void Main(string[] args)
		{
			List<Info> students = new List<Info>();

			bool again = true;
			Console.Clear();

			Console.WriteLine("Please input your student information: ");

			  while (again)  
			 {  
				 bool dupIdNum = false;  
   
				 Info newStudent = new Info();  
   
				 Console.Write("\nFirst Name: ");  
				 string firstName = Console.ReadLine();  
				 newStudent.firstName = firstName;  
   
				 Console.Write("Last Name: ");  
				 string lastName = Console.ReadLine();  
				 newStudent.lastName = lastName;  
   
				 Console.Write("Id: ");  
				 string input3 = Console.ReadLine();  
				 int idNum = Convert.ToInt32(input3);  
				 newStudent.idNum = idNum;  
   
				 foreach (Info st in students)  
				 {
					 if (st.idNum == newStudent.idNum)
					 {
						 dupIdNum = true;

						 Console.Write("KSU Id already exists in the system, please input a new one: ");
						 string newInput = Console.ReadLine();
						 int newIdNum = Convert.ToInt32(newInput);
						 newStudent.idNum = newIdNum;
						 continue;
					 }
					 else
						 continue;
				 }
				 
				 if (dupIdNum == false)  
					 students.Add(newStudent);  
   
				 Console.WriteLine("\n\nMore Students? (y/n)");  
				 char input4 = Console.ReadKey().KeyChar;  
   
				 if (input4 == 'n')  
					 again = false;  
				 else if (input4 == 'y')  
					 again = true;  
				 else  
				 {  
					 Console.WriteLine("You have not entered a valid response. Goodbye.");  
					 again = false;  
				 }  
			 }  


Was This Post Helpful? 0
  • +
  • -

#5 Onker   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 15-July 08

Re: Preventing Duplicate values in a list.

Posted 10 October 2008 - 06:43 AM

I've been looking everything over, and I'm of the opinion that if I can figure out the Struct portion (how to make it malleable so I can have a varying number of "homework" fields) I should be able to figure the rest of it out.

Can anyone help me figure that part out? Is it something I need to be doing IN the struct?
Was This Post Helpful? 0
  • +
  • -

#6 Onker   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 15-July 08

Re: Preventing Duplicate values in a list.

Posted 10 October 2008 - 07:52 AM

As is frequently the case, my problem was a lot more difficult than it should have been...because of a failure in reading comprehension.

I don't have to store the individual grades to the struct, I have to process them and store the overall grade to the struct...which is one field...which is easily doable with a minor conversion to my existing set up.
/facepalm.

That being said, I can now do everything I need to I believe.

Thanks again to Jeff, for not only pointing out my mistake but giving me a beautifully functional bit of code to play with.

And thanks to those who popped into the thread to see if they could help as well. Not your fault I can't read what my professor wants.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1