0

I am making a program that takes the Username, Age and ID of an user and then prints them out to the screen. The username can't contain any symbols or spaces (except _). So, I made a function that returns true if the name has symbols in it and false if it doesn't. However I am getting an error during compiling: No overload for method 'Exists' takes '1' arguments. The full error:

challenge_2.cs(23,37): error CS1501: No overload for method `Exists' takes `1' arguments
/usr/lib/mono/2.0/mscorlib.dll (Location of the symbol related to previous error)
Compilation failed: 1 error(s), 0 warnings

Here is the code:

using System;
using System.Collections.Generic;

public class Challenge_2
{
    static string myName;
    static string myAge;
    static string myUserID;
    public static char[] break_sentence(string str)
    {
        char[] characters = str.ToCharArray();
        return characters;
    }
    public static bool check_for_symbols(string s)
    {
        string[] _symbols_ = {"!","@","#","$","%","^","&","*","(",")"," ","-","+","=","~","`","\"","'","{","}","[","]","\\",":",";","<",">","?","/",","};
        List<string> symbols = new List<string>(_symbols_);
        char[] broken_s = break_sentence(s);
        int _bool_ = 0;
        for(int i = 0; i < symbols.Count; i++)
        {
            string current_symbol = symbols[i];
            if(broken_s.Exists(current_symbol))
            {
                _bool_ = 1;
                break;
            }
        }
        if(_bool_ == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    public static void Main()
    {
        Console.WriteLine("Please answer all questions wisely.");
        Console.WriteLine(" ");
        name();
        Console.WriteLine(" ");
        age();
        Console.WriteLine(" ");
        userID();
        Console.WriteLine(" ");
        string nextAge = Convert.ToString(Convert.ToInt32(myAge)+1);
        string nextID = Convert.ToString(Convert.ToInt32(myUserID)+1);
        Console.WriteLine("You are {0}, aged {1} next year you will be {2}, with user id {3}, the next user is {4}.", myName, myAge, nextAge, myUserID, nextID);
    }
    public static void name()
    {
        Console.WriteLine("What is your forum name?");
        Console.Write(">> ");
        myName = Console.ReadLine();
        while(check_for_symbols(myName) == true)
        {
            Console.WriteLine("Name can't contain symbols/spaces.");
            Console.Write("Please enter a valid forum name: ");
            myName = Console.ReadLine();
        }
    }
    public static void age()
    {
        Console.WriteLine("What is your age?");
        Console.Write(">> ");
        myAge = Console.ReadLine();
        while(Convert.ToInt32(myAge) <= 0 || Convert.ToInt32(myAge) > 120)
        {
            Console.WriteLine("That isn't a valid age.");
            Console.Write("Please enter a valid age: ");
            myAge = Console.ReadLine();
        }
    }
    public static void userID()
    {
        Console.WriteLine("What is your User ID?");
        Console.Write(">> ");
        myUserID = Console.ReadLine();
        while(Convert.ToInt32(myUserID) <= 0 || Convert.ToInt32(myUserID) > 999999)
        {
            Console.WriteLine("UserID must be in the range: 0 < x < 1000000.");
            Console.Write("Please enter a valid user ID: ");
            myUserID = Console.ReadLine();
        }
    }
}

Any help is appreciated.

sirius_x
  • 183
  • 4
  • 11

4 Answers4

5

replace this part of your function

        string current_symbol = symbols[i];
        if(broken_s.Exists(current_symbol))
        {
            _bool_ = 1;
            break;
        }

into

        string current_symbol = symbols[i];
        if(broken_s.Contains(current_symbol))
        {
            _bool_ = 1;
            break;
        }

cheers!

Zerratar
  • 1,229
  • 11
  • 10
  • 1
    broken_s is char array so IT WON'T WORK, Contains is waiting for char but gets string -> error – Kamil Budziewski Aug 13 '13 at 10:52
  • 1
    Doesn't work. Error: error CS1061: Type 'char[]' does not contain a definition for 'Contains' and no extension method 'Contains' of type 'char[]' could be found (are you missing a using directive or an assembly reference?) – sirius_x Aug 13 '13 at 11:08
  • @Stormboy - Make sure you have imported the system.linq namespace. The Contains method is an extension method on IEnumerable. Since char[] implements IEnumerable, the Contains method will be there. However, `string current_symbol` should be `char current_symbol` – Chris Dunaway Aug 13 '13 at 17:23
2

Maybe try this code:

        char[] _symbols_ = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', ' ', '-', '+', '=', '~', '`', '\'', '\'', '{', '}', '[', ']', '\\', ':', ';', '<', '>', '?', '/', ',' };
        List<char> symbols = new List<char>(_symbols_);
        char[] broken_s = break_sentence(s);
        int _bool_ = 0;
        for (int i = 0; i < symbols.Count; i++)
        {
            char current_symbol = symbols[i];
            if (broken_s.Any(x=>x==current_symbol))
            {
                _bool_ = 1;
                break;
            }
        }

Because you are mixing up strings and chars you need to change your array into char array and then you can check if it contains forbidden symbols

You can also modify your code a little to remove useless loop:

        List<char> symbols = new List<char>(_symbols_);
        char[] broken_s = break_sentence(s);
        int _bool_ = 0;
        if(broken_s.Any(x=>symbols.Contains(x)) _bool=1;
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • It also gives an error: error CS1061: Type 'char[]' does not contain a definition for 'Any' and no extension method 'Any' of type 'char[]' could be found (are you missing a using directive or an assembly reference?) – sirius_x Aug 13 '13 at 11:06
  • 1
    @Stormboy add `using System.Linq` to your code: http://stackoverflow.com/questions/2718276/how-to-use-linq-in-mono – Kamil Budziewski Aug 13 '13 at 11:08
1

I am not sure about Mono but in Microsoft.NET the signature for Exists is:

T[] array, Predicate<T>

Which means that you can use it as such:

    var testCharArray = new[] {'a','b'};
    var condition = Array.Exists(testCharArray, c => c.Equals('b'));

This works with strings too:

    var testStringArray = new[] { "anders", "calle" };
    var condition2 = Array.Exists(testStringArray, c => c.Equals("calle"));
Marcus
  • 8,230
  • 11
  • 61
  • 88
  • It compiles but the code still works if I enter "Hello!@World" as the name. It should give the message: "Name can't contain symbols/spaces.". – sirius_x Aug 13 '13 at 11:14
  • I was just showing how to do the comparison, the exclusion I left for you to implement :P – Marcus Aug 13 '13 at 11:19
1

Another option is to use the String.IndexOfAny() method which takes an char array as parameter like:

    public static bool check_for_symbols(string s)
    {

        return ("!@#$%^&*() -+=~`\"'{}[]\\:;<>?/,".IndexOfAny(s.ToCharArray()) > -1);

    }
Heslacher
  • 2,167
  • 22
  • 37