Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
class Program
    {
        static void Main(string[] args)
        {
            char c = 'x';
            Console.Write("Input string: ");
            string str = Console.ReadLine();
            for (int i = 0; i < str.Length; i++)
            {
                c = str[i];
                if (System.Char.IsSymbol(c) || System.Char.IsSeparator(c) || System.Char.IsWhiteSpace(c))
                {
                    Console.WriteLine("Integers and identifiers can not be symbols, space or contain them");
                }
            }
            int a;
            bool check = int.TryParse(str, out a);
            if (check == true)
            {
                Console.WriteLine("The input string is an integer.");
            }
            if (check == false)
            {
                Console.WriteLine("The input string is an identifier.");
            }
        }
    }
}


What I have tried:

The problem is : " Create a method, which checks if input string is an Integer or Identifier." My code seems to be not right because when I input "abc abc" or "$abc" as a string the output is :

Integers and identifiers can not be symbols, space or contain them
The inputed string is an identifier.
Press any key to continue . . .
Posted
Updated 16-Nov-17 23:58pm
Comments
CHill60 17-Nov-17 5:50am    
When you have finished your loop your code goes straight into the check for integer. Try adding a bool to flag that an error is already found and surround your integer/identifier check with an if statement
BillWoodruff 17-Nov-17 19:33pm    
There are different ways you could interpret "the problem" as you describe it.

I suspect the "correct" (what your course asks for) solution does not allow the user to enter an entire line of text at a time. I can't imagine doing that in "real world" code.

Please clarify. Ask me, if you want to see a diffewrent technique.

The problem is that if you find a problem in your string, you don't do anythign about it, other than tell the user.
Add a return to the if code:
c = str[i];
 if (System.Char.IsSymbol(c) || System.Char.IsSeparator(c) || System.Char.IsWhiteSpace(c))
 {
     Console.WriteLine("Integers and identifiers can not be symbols, space or contain them");
     return;
 }
And it should work.
But I'd either use a foreach loop instead of the for:
C#
foreach (char c in str)
   {
   ...
   }
Or use the the built in method Any (but that may be a bit advanced for you at this stage):
if (str.Any(c => char.IsSymbol(c) || char.IsSeparator(c) || char.IsWhiteSpace(c)))
    {
    Console.WriteLine("Integers and identifiers can not be symbols, space or contain them");
    return;
    }
 
Share this answer
 
Comments
Member 13504137 17-Nov-17 6:19am    
Thank you! That was useful and answered some other questions. :)
Try something like this (untested)
ass Program
{
    static void Main(string[] args)
    {
        char c = 'x';
        Console.Write("Input string: ");
        string str = Console.ReadLine();
                
		if ValidInput(str)
		{
			int a;
			bool check = int.TryParse(str, out a);
			if (check)
			{
				Console.WriteLine("The input string is an integer.");
			}
			else
			{
				Console.WriteLine("The input string is an identifier.");
			}
		}
		else
		{
			Console.WriteLine("Integers and identifiers can not be symbols, space or contain them");
		}
    }
	bool ValidInput(string x)
	{
		ValidInput = true;
		for (int i = 0; i < str.Length; i++)
        {
            c = str[i];
            if (System.Char.IsSymbol(c) || System.Char.IsSeparator(c) || System.Char.IsWhiteSpace(c))
            {
                ValidInput = false;
				exit for;
            }
        }
	}
}

Note you do not need = true for bools. And if a bool is not true then it must be false - so no need for the second if
 
Share this answer
 
Comments
Member 13504137 17-Nov-17 6:14am    
Thank you ^_^. Found this helpful not just for this problem.

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