Hello in C#. I need help understanding and knwoing what i missed in my program. The issue is, the program is supposed to have user input for 12 family members and at the end of the 12 it asks for user to input a name to search for. When i put a correct name, it always gives me a relative not found message. WHat did i miss. And can you adjust the new code so im able to see where it shouldve been.
Thabk you
using System;
namespace RelativesList
{
class Relative : IComparable
{
private string name;
private string relationship;
private int day;
private int month;
private int year;
public int CompareTo(object obj)
{
if (obj == null) return 1;
Relative reIOBJ = obj as Relative;
if (reIOBJ != null)
return this.Name.CompareTo(reIOBJ.Name);
else
throw new ArgumentException("Argument Exception");
}
public Relative()
{
name = "";
relationship = "";
day = 1;
month = 1;
year = 2001;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Relationship
{
get
{
return relationship;
}
set
{
relationship = value;
}
}
public int Day
{
get
{
return day;
}
set
{
day = value;
}
}
public int Month
{
get
{
return month;
}
set
{
month = value;
}
}
public int Year
{
get
{
return year;
}
set
{
year = value;
}
}
public override string ToString()
{
string output =
String.Format("Name {0} | Relationship:"
+ "{1} | DOB: {2:D2}-{3:D2}"
+ "-{4:D4}\n", name,
relationship, day, month, year);
return output;
}
}
class Program
{
static void Main(string[] args)
{
const int NUMBER = 12;
Relative[] relatives = new Relative[NUMBER];
for (int idx = 0; idx < NUMBER; idx++)
{
relatives[idx] = new Relative();
Console.Write("Enter name: ");
relatives[idx].Name = Console.ReadLine();
Console.Write("Enter relationship:");
relatives[idx].Relationship =
Console.ReadLine();
Console.Write("Enter day of birth: ");
relatives[idx].Day = Convert.ToInt16
(Console.ReadLine());
Console.Write("Enter month of birth: ");
relatives[idx].Month = Convert.ToInt16
(Console.ReadLine());
Console.Write("Enter year of birth: ");
relatives[idx].Year = Convert.ToInt16(Console.ReadLine());
Console.WriteLine();
}
Array.Sort(relatives);
Console.WriteLine("Relative Data sorted by name:\n");
for (int idx = 0; idx < NUMBER; idx++)
{
Console.WriteLine(relatives[idx].ToString());
}
Console.Write("\nEnter Relative's Name: ");
string name = Console.ReadLine();
int index = 0;
bool found = false;
for (index = 0; index < NUMBER; index++)
{
if (relatives[index].Name.CompareTo(name) == 0)
{
found = true;
break;
}
}
if (!found)
Console.WriteLine("Relative not found");
else
{
Console.WriteLine("Relationship: " + relatives[index].Relationship);
Console.WriteLine(
String.Format("DOB: {0}-{1}-{2}", relatives[index].Day, relatives[index].Month, relatives[index].Year));
}
Console.ReadKey();
}
}
}
The issue is very simple, The code doesn't handle the comparison to compare the same strings(names) in different cases.
For example: in the code given, If the relative name is "George" and the name to be searched is "george" it won't consider it as same.
The code has been checked with random input and found this is the issue.
The statement to be changed is the comparison statement
if (relatives[index].Name.CompareTo(name) == 0)
There are two ways to fix this issue:
1. To use any case conversion method (ToUpper() or ToLower)
2. To use Equals with StringComparison.OrdinalIgnoreCase
Using ToUpper and ToLower
This is a very common method used to compare strings as case insensitive.
The changed statement will be
Using Equals with StringComparison.OrdinalIgnoreCase
Hope this helps!!!
Hello in C#. I need help understanding and knwoing what i missed in my program. The...
i need help fixing my code. here is the assignment: Make a class that contains the following functions: isLong, isDouble, stringToLong, and stringToDouble. Each receives a string and returns the appropriate type (bool, bool, long, and double).During the rest of the semester you will paste this class into your programs and will no longer use any of the "standard" c# functions to convert strings to numbers. here is my code. it works for the long method but not the double:...
C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employees to be compared by the Name, Age, and Pay, respectively. Code: Employee.Core.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Employees { partial class Employee { // Field data. private string empName; private int empID; private float currPay; private int empAge; private string empSSN = ""; private string empBene...
What I need: Write a program named Averages that includes a method named Average that accepts any number of numeric parameters, displays them, and displays their average. For example, if 7 and 4 were passed to the method, the ouput would be: 7 4 -- Average is 5.5 Test your function in your Main(). Tests will be run against Average() to determine that it works correctly when passed one, two, or three numbers, or an array of numbers. What I...
[C#] I need to convert this if/else statements into Switch I have the code for the if/else but now need to turn it into a switch. The Console.WriteLine and all that is fine. I just need to convert the if/else. int x1, x2, y1, y2; Console.WriteLine("What is smallest value of x:"); x1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("What is largest value of x:"); x2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("What is smallest value of y:"); y1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("What is largest value of y:"); y2 =...
I need help with my code. It keeps getting this error: The assignment is: Create a class to represent a Food object. Use the description provided below in UML. Food name : String calories : int Food(String, int) // The only constructor. Food name and calories must be // specified setName(String) : void // Sets the name of the Food getName() : String // Returns the name of the Food setCalories(int) : void // Sets the calories of the Food...
This C# program prints out a corresponding letter grade for a given mark. It uses a method (called MarktoGrade) to determine the letter grade. MarktoGrade takes one integer call by value formal parameter (called mark) and returns a char value that is the letter grade. All of the input and output (except an error message) is done in Main. There is one syntax error in this program that you will need to fix before it will compile (the error is...
C#, I am getting error placing them all in one file pls set them in the order that all 3 work in one file. Thanks //Program 1 using System; class MatrixLibrary { public static int[,] Matrix_Multi(int[,] a, int [,]b){ int r1= a.GetLength(0); int c1 = a.GetLength(1); int c2 = b.GetLength(1); int r2 = b.GetLength(0); if(r2 != c2){ Console.WriteLine("Matrices cannot be multiplied together.\n"); return null; }else { int[,] c = new int[r1, c2]; for (int i = 0; i <...
My values are not storing into my object and I keep returning zero. please explain my mistake thanks public class CalanderDate { int year = 0 ; int day = 0; int month= 0; public static void main(String[] args) { CalanderDate date; date = new CalanderDate( 1, 10 ,1999); System.out.print(date); } public CalanderDate(int month, int day , int year ) { if (month < 1){ month = 1; } if (month> 12){ month = 12; }...
Greetings everyone I am having a little trouble with this one. I bolded my switch case which should work but its currently not. If a user selected course option 1 twice it just says you enrolled in course 1 and course 1. It should stop it by executing case -2. Need a new set of eyes. - Thanks using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleRegisterStudent { class Program { static void Main(string[] args) { (new...
Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...