Reputation: 119
This is a C# code to enter 10 names, sort them in ascending order and print them with the condition if either of the elements are "Nobody" or "Somebody" it should skip that name when printing. The issue with my code is that it doesn't skip those 2 mentioned words and prints them as well, I don't understand why.
Sample Input:
Ravi
Somebody
Tanvir
Ramesh
Nobody
Ani
Nobody
Vishwanath
Somebody
Nitin
Sample Output:
Ani
Nitin
Ramesh
Ravi
Tanvir
Vishwanath
using System;
using System.Collections;
namespace LearnCsharp
{
class NamesWithArrayList
{
public static void Main(string[] args)
{
//Update the code below
ArrayList alObj;
alObj = new ArrayList();
int max=10;
string item="";
for(int i=0;i<max;i++)
{
alObj.Add(Console.ReadLine());
}
alObj.Sort();
foreach (string item1 in alObj)
{
if(alObj.Contains("Somebody")){}
else if(alObj.Contains("Nobody")){}
else
Console.WriteLine(item1);
}
}
}
}
Upvotes: 1
Views: 88
Reputation: 19340
In 2020 you don't need to use ArrayList
. In fact the only reason it is still exists probably is backward compatibility. Use List<string>
. If you do that, you can do this
using System.Linq;
myList.Where(x => x != "Nobody" && x != "Somebody")
.Sort(StringComparer.OrdinalIgnoreCase)
.ToList()
.ForEach(x => Console.WriteLine(x));
Another interesting way is this
using System.Linq;
var checkList = new List<string>(){ "Nobody", "Somebody" };
myList.Except(checkList)
.Sort(StringComparer.OrdinalIgnoreCase)
.ToList()
.ForEach(x => Console.WriteLine(x));
In this instance you work on the lever of 2 lists. You will retrieve only items that don't match.
Upvotes: 0
Reputation: 1970
The problem is that on the if(alObj.Contains("Somebody")){}
line you're asking if the original alObj
ArrayList
contains the string "Somebody". Your foreach
loop should be re-written as follows:
foreach (string item1 in alObj)
{
if(item1 == "Somebody")
{
}
else if (item1 == "Nobody")
{
}
else
Console.WriteLine(item1);
}
However this could be improved further by merging those the "Somebody" and "Nobody" cases into one:
foreach (string item1 in alObj)
{
if(item1 == "Somebody" || item1 == "Nobody")
{
// Do nothing
}
else
Console.WriteLine(item1);
}
Finally this is a bit weird looking, the real intent of the code is "Only output the item if its not equal to 'Somebody' and not equal to 'Nobody'", which is best expressed like so:
foreach (string item1 in alObj)
{
if(item1 != "Somebody" && item1 != "Nobody")
Console.WriteLine(item1);
}
Upvotes: 1
Reputation: 293
You need to check '''item1''' if it is your searched string and you need to put some code for the if statement what your app should do than.
Upvotes: 1
Reputation: 152556
"Somebody"
each time rather than if the current value is "Somebody"
.ArrayList
is essentially a collection of object
s, so string comparison is not being used when calling Contains
. It's instead using object reference comparison, and since your inputs are different objects that the string constants, the comparison always fails.So a version which uses string comparisons would be:
foreach (string item1 in alObj)
{
if(item1 == "Somebody"){}
else if(item1 == "Nobody"){}
else
Console.WriteLine(item1);
}
which could be simplified as:
foreach (string item1 in alObj)
{
if(item1 != "Somebody" && item1 != "Nobody")
Console.WriteLine(item1);
}
Upvotes: 2