Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;

namespace ADP_PG2_1_Solution
{
	public class IsDualNumberExercise
	{
		public static bool IsDualNumber(string s) {
           
            //User imput 
            Console.WriteLine("Bitte geben Sie eine Zahl:" + s);
            s = Console.ReadLine();

            //We need now to compare the above given number to "1" or "0"
            for(int i =0; i < s.Length; i++)
            {
               if(s[i] == 1 || s[i] == 0)
                {
                    Console.WriteLine("The number you've given is a boolean",s);
                }
                else
                {
                    // if(s ist neighter of them)
                    Console.WriteLine("Is not a boolean");
                }
            }

			return false;
		}
	}
}


What I have tried:

It doesnt work :-( Can someone plse tell me why ?
Thanks
Posted
Updated 25-Apr-16 22:15pm
Comments
Sergey Alexandrovich Kryukov 26-Apr-16 4:03am    
0 or 1 is not Boolean. Do you mean, as integer? :-)
—SA
Member 12485203 28-Apr-16 15:29pm    
thanks i got it already (Y)
Patrice T 26-Apr-16 5:25am    
Define "doesn't work".
Patrice T 26-Apr-16 5:29am    
Are you talking about boolean or base 2 number ?
Member 12485203 28-Apr-16 15:30pm    
thanks i got it already

That's some odd code: You are passing a string into the method but immediately overwriting it. You can't make the new string available to the method that calls IsDualNumber because strings are immutable (which means that they can't be changed - a new string is generated each time you try) and because what you pass is a reference to the string rather than the string content. So passing a string in is pointless!
Instead, I'd read the string in the calling method, and pass that to the IsDualNumber method.
Then, you have the next problem - which is that a string is a Collection of char values. So when you use an index, to retrieve a value it returns a single char - and '1' is not the same as 1, '0' is not the same as 0.
Your method always returns a false - so it's not really a bool at all!
I'd do it differently:
C#
string s = Console.ReadLine();
if (IsDualNumber(s))
   {
   Console.WriteLine("The number \"{0}\" is a boolean", s);
   }
else
   {
    Console.WriteLine("\"{0}\" is not a boolean", s);
   }
...
public static bool IsDualNumber(string s) 
    {
    foreach (char c in s)
        {
        if(c != '1' && c != '0')    // false if NOT '1' and NOT '0' 
            {
            return false;
            }
        }
    return true;
    }
 
Share this answer
 
v2
Comments
Matt T Heffron 26-Apr-16 13:38pm    
FTFY:
You had logical OR where you meant logical AND.
(Logical OR would always test as true.)
OriginalGriff 26-Apr-16 13:57pm    
:doh:
Well done...good spot!
George Swan 26-Apr-16 17:20pm    
bool isBool = (testString == "0" || testString == "1");
OriginalGriff 27-Apr-16 1:57am    
The only reason I didn't suggest that was that his original checks each character, not the whole string - I suspect that this is supposed to be a "binary number" checker not a "bool" checker and he has his terms wrong.
When you do a readline, it is a string.
You might want to consider the below:

if(s[i] == "1" || s[i] == "0")

OR

if(Convert.ToInt(s[i]) == 1 || Convert.ToInt(s[i]) == 0)

Hope it helps.
You can also try converting to boolean but you will have to handle the exception when it is not 1 or 0.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900