Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Always getting first value as false while searching a number?

What I'm trying to do is to allow the user to search how many false or true values are saved in a bool Vektor It's also important that the program should handle input errors.

I guess I have to use Convert.Boolean but I don't know how. At the moment I'm keep getting the same thing whether I search numbers or letter.

static void Main(string[] args)
{
    Random newRandom = new Random();
    int slumpTal = newRandom.Next(1, 101);
    bool[] boolVektor = new bool[slumpTal];

    //var nacas = Convert.ToBoolean(Convert.ToInt32("0"));

    for (int i = 0; i < boolVektor.Length; i++)
    {
        int slump = newRandom.Next(0, 2);
        if (slump == 0)
            boolVektor[i] = true;
        else
            boolVektor[i] = false;
    }
    {
        Console.Write("Skriv in sökOrd: ");
        string searchWord = Console.ReadLine();
        bool search = false;

        for (int i = 0; i < boolVektor.Length; i++)
        {
            if (boolVektor[i] == search)
            {
                Console.WriteLine("The following were found: " + boolVektor[i]);
                search = true;
            }
            if (!search)
            {
                Console.WriteLine("Your search failed");
            }
        }
        Console.ReadLine();
    }
}
like image 372
Samakaab Avatar asked Nov 22 '17 08:11

Samakaab


2 Answers

To search for a value of a certain data type in an array, you need to convert the user input into that datatype. Then you go ahead and compare the converted value in the same manner as you do it now.

The conversion can be done the following way:

Console.Write("Skriv in sökOrd: [TRUE|FALSE]");
string searchWord = Console.ReadLine();
bool search = Convert.ToBoolean(searchWord);

bool foundAnyMatches = false


for (int i = 0; i < boolVektor.Length; i++)
{
    if (boolVektor[i] == search)
    {
        Console.WriteLine("The following were found: " + boolVektor[i] + 
        "Index: " + i);
        foundAnyMatches = true;
    }
}
if (!foundAnyMatches)
{
    Console.WriteLine("Your search failed");
}

and please don't change the search value! because you use it as the search condition!

EDIT:

As for the handling of wrong input you can put the conversion into a try/catch block or you can use the Boolean.TryParse() method

like image 153
Mong Zhu Avatar answered Oct 10 '22 15:10

Mong Zhu


I made your current method working and explained some stuff in the comments:

Random newRandom = new Random();
int slumpTal = newRandom.Next( 1, 101 );
bool[] boolVektor = new bool[ slumpTal ];

for ( int i = 0; i < boolVektor.Length; i++ )
{
    int slump = newRandom.Next( 0, 2 );
    if ( slump == 0 )
        boolVektor[ i ] = true;
    else
        boolVektor[ i ] = false;
}

Console.Write( "True/False: " );
bool search = Convert.ToBoolean(Console.ReadLine()); //Thanks Mong Zhu
bool foundMatches = false;

for ( int i = 0; i < boolVektor.Length; i++ )
{
    if ( boolVektor[ i ] == search )
    {
        //If you do boolVektor[i] it will just show true/false
        Console.WriteLine( $"The following index is found: {i} " );
        foundMatches = true;
    } 
}
if ( !foundMatches ) //We check if the search failed here because now the search is finished
{
    Console.WriteLine( "Your search failed" );
}
Console.ReadLine();

If you want to count the number of occurrences of the user input then replace the for loop by this:

int count = boolVektor.Where( row => row == search ).Count();
if(count != 0)
{
    Console.WriteLine( $"{count} items were found" );
}
else
{
    Console.WriteLine( "Your search failed" );
}
Console.ReadLine();
like image 29
EpicKip Avatar answered Oct 10 '22 13:10

EpicKip