Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Here's the code..
C#
if (code.StartsWith("0") || code.StartsWith("1") ||
    code.StartsWith("2") || code.StartsWith("3") ||
    code.StartsWith("4") || code.StartsWith("5") ||
    code.StartsWith("6") || code.StartsWith("7") ||
    code.StartsWith("8") || code.StartsWith("9") ) //to test if number
{
    sNum = String.Concat(sNum, code.Substring(0, 1)); //gets the first character
    if ((code[1].Equals("0") && code[1].Equals("1") &&
         code[1].Equals("2") && code[1].Equals("3") &&
         code[1].Equals("4") && code[1].Equals("5") &&
         code[1].Equals("6") && code[1].Equals("7") &&
         code[1].Equals("8") && code[1].Equals("9")) == false)
    {
        richTextbox1.AppendText("INTEGER: "+sNum);
    }
    code = code.Remove(0, 1); //removes the first character
}


the code supposed to do this..

for example

code = "19 * 12"

then the output should be..

INTEGER: 19
INTEGER: 12

hehehe can someone tell me what am i doing wrong..haha X|
Thank you in advance ! and HAPPY 2011 EVERYONE !

[edit]Code block corrected and tidied, subject - OriginalGriff[/edit]
Posted
Updated 1-Jan-11 4:08am
v2
Comments
Smithers-Jones 1-Jan-11 10:04am    
Still drunk from last night's party or why does your post contain all these "hehehes"?

modified: Well, at least Griff removed them from the subject line now.
Toli Cuturicu 2-Jan-11 8:12am    
Awful!

There are a few problems here: the most blatant is your second if condition.
if ((A == 1) && (A == 2))
will always give false...
 
Share this answer
 
If all you are trying to do is get the numbers out as discrete items, you can use a regular expression. Here's the code that would do this:
C#
public static Regex regex = new Regex(
      "\\d*",
    RegexOptions.Multiline
    | RegexOptions.Compiled
    );
MatchCollection ms = regex.Matches(InputText);
 
Share this answer
 
Well, your code ads it sits now) is not going to work because you're assuming that all numbers are two digits long. Beyond that, there's a better way to determine if ta certain character is a digit

C#
private bool IsNumeric(char value)
{
    int outValue;
    bool isNumeric = Int32.TryParse(value, out outValue);
    return isNumeric;
}


At this point, you could do this:

C#
string numberStr = "";
foreach (char character in code)
{
    if (IsNumeric(character))
    {
        numberStr += character;
    }
}


As for parsing the rest of the string appropriately (to extract numeric values), I'll leave that as an exercise for you.

 
Share this answer
 
Comments
Shani Natav 1-Jan-11 14:15pm    
Why not <pre>char.IsDigit(character)></pre>?
you can try this:

MatchCollection split = Regex.Matches(code, "[0-9]+");
string[] integers = split.OfType<Match>().Select(a => "INTEGER: " + int.Parse(a.Value)).ToArray();
 
Share this answer
 
v4
See code below:

MIDL
if (code.StartsWith("0") || code.StartsWith("1") ||
    code.StartsWith("2") || code.StartsWith("3") ||
    code.StartsWith("4") || code.StartsWith("5") ||
    code.StartsWith("6") || code.StartsWith("7") ||
    code.StartsWith("8") || code.StartsWith("9") ) //to test if number
{
    sNum = String.Concat(sNum, code.Substring(0, 1)); //gets the first character
    if (!(code[1].Equals("0") || code[1].Equals("1") ||
         code[1].Equals("2") || code[1].Equals("3") ||
         code[1].Equals("4") || code[1].Equals("5") ||
         code[1].Equals("6") || code[1].Equals("7") ||
         code[1].Equals("8") || code[1].Equals("9")))
    {
        richTextbox1.AppendText("INTEGER: "+sNum);
    }
    code = code.Remove(0, 1); //removes the first character
}


If you had thought about it you should have found out. The way you wrote the second if in your snippet it always came out as false.

Regards,
Manfred
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Jan-11 14:01pm    
Manfred, no vote for you from me this time. You should have removed that stupid Equals.
Will you care to fix this, too?
(I'm personally too lazy to write any code for this trivial problem -- what should be fixed in real life is not exactly this piece of code...)
Manfred Rudolf Bihy 1-Jan-11 14:21pm    
@SAKryukow: I was only trying to show him where his logical error was, in no way would I waste my time pointig out all the nasty no-nos he committed. JSOP, Pete, Shani and you already pointed that out to him. It's your descision on what you vote :). Happy new year, BTW!
On more issue:

Just using explicit call to the (virtual!) method Equals (why? should be comparison operator) shows lack of understanding of object identity and comparison issues.

Starting from this point, the author of the code is just bound to having various mistakes; then number of them is just a matter of temperament. ;)
 
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