Hey!

I'm currently writing a program which converts a number from binary to int. That part is done, but I'm now trying to check wheither the input string only consists of 1 and 0.

This is what I've tried so far, but it doesnt work the way I want.

string binarynumber;
bool binaryLoop = true;
Console.Writeline("Please type in a binary number: ");

while (binaryLoop)
{
try
{
foreach (char digit in binaryNumber)
{
int digit = Convert.ToInt32(siffer.ToString());
if (digit == 1 || digit == 0)
{
binaryLoop = false;
}
else
{
Console.WriteLine("The binary number can only be 1 and 0, please retype");

}

}
}
catch(FormatException)
{
Console.WriteLine("The binary number can only be 1 and 0, please retype");;
}
}

Any hint? Or better methods?

Regards Asotop.

hi, check this, just changed your code.

string binarynumber;
bool binaryLoop = true;
Console.WriteLine("Please type in a binary number: ");

while (binaryLoop)
{
try
{
foreach (char digit in binarynumber)
{
//int digit = Convert.ToInt32(siffer.ToString());
if (digit == '1' || digit == '0')
{
binaryLoop = false;
}
else
{
//Console.WriteLine("The binary number can only be 1 and 0, please retype");
throw new FormatException();

}

}
Console.WriteLine("works fine");
}
catch(FormatException)
{
Console.WriteLine("The binary number can only be 1 and 0, please retype");
}
}

Put a break after line 15, there is no need to continue checking if the rest of the digits are 0 or 1 after you've found one that fails.

You could also use a Regex

if (Regex.Match(binarynumber, "^[01]+\$") == null) {
// not a binary number
} else {
// is a binary number
}

Or a bit more suphisitcated, using checking if char is number (and a bit different checking of binary data):

bool bExit = false;
Console.WriteLine("Please type in a binary number: ");
string binarynumber = Convert.ToString(Console.ReadLine());
while (!bExit)
{
foreach (char digit in binaryNumber)
{
if(char.IsDigit(digit)
{
int digit = Convert.ToInt32(new string();
if (digit != '1' || digit != '0')
{
Console.WriteLine("The binary number can only be 1 and 0, please retype");
bExit = true; //char is not 0 or 1
break;
}
}
else
bExit = true; // char is not a number!
}
break; // all ok!
}
if(!bExit)
{
// when code exits the loop, you can continue working, it will
}

Thanks alot, still learning about the different possibilities with Regex, I'll look into each solution.

@arunkumars

It's almost working now, but if i type in, lets say 10201, it will still go through.

Maybe I shouldent use a foreach loop at all? I won't use the Regex, cause my teacher doesn't approve of it, he prefers doing it the hard way :P

Check this out:

bool bExit = false;
bool bReType = false;
Console.WriteLine("Please type in a binary number: ");
while (!bExit)
{
while (!bReType)
{
string binarynumber = Convert.ToString(Console.ReadLine());
foreach (char digit in binarynumber.ToCharArray())
{
if (char.IsDigit(digit))
{
int myInt = int.Parse(digit.ToString());
if (myInt != 1 && myInt != 0)
{
Console.WriteLine("The binary number can only be 1 and 0, please re-type");
bReType = true;
break;
}
}
else
{
Console.WriteLine("The char is not a number, please re-type.");
bReType = true;
break;
}
}
if (!bReType)
{
bReType = true;
bExit = true;
}
}
bReType = false;
}
// when code exits the loop, you can continue working, it will

Thanks alot.

@Asotop,
its working fine for me, i tested n then pasted that code, may be u can paste the code that u have included, i can check.
if my ans helped u, kindly click on the reputation points..
bye.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.