You must Sign In to post a response.
  • Category: Visual Studio

    C# if statement to see how many comma's in input

    SO I have a string called 'input' and I'm trying to do an if statement regarding to see if the current equals the minus sign and current string index is not the only valid index position for entry is valid and loop should stop.

    Also, I'm trying to do an if statement to see if the current character is equal to the decimal point by incrementing a counter to track the number of decimal places. I have no idea on how to go about either one. Please help. By the way I'm a beginner so I'm trying to only use arrays, int, for loop, bool, double, methods, and strings
  • #752599
    Hi Christina,

    You are using if statement in wrong place. To validate the input string or value you have so many other simple options in .NET.

    You may either use regular expression or javaScript to validate the input string.

    If you can let me know what kind of validation you wanted to do I can provide you the sample code.

    Just let me know the expected input from the user so that I can write the code for you.

    Asheej T K

  • #752605
    So this is what I'm trying to do: In order to look at each character we will treat the string as an array of characters (since that is what a string is).
    We will need a loop to look at each character and decide whether the character is valid.
    As soon as an invalid character is found we will stop executing the loop because there is no need to continue looking at the characters in the string once a single character is invalid.
    The loop will continue to execute until an invalid character is found or the end of the string array is reached.

    My code so far is :

    Console.WriteLine("Simple Calculator");
    Console.WriteLine("\t 1) Add");
    Console.WriteLine("\t 2) Subtract");
    Console.WriteLine("\t 3) Multiply");
    Console.WriteLine("\t 4) Divide");
    Console.WriteLine("\t 5) Quit");

    Console.Write("\n Enter Selection: ");
    menuSelection = Console.ReadLine();
    number = Convert.ToInt32(menuSelection);
    switch (menuSelection)
    case "1":
    number = 0;
    case "2":
    number = 5;
    case "3":
    menuSelection = "*";
    case "4":
    menuSelection = "/";
    case "5":
    Console.WriteLine("Invalid Selection. Try Again");

    if ( number >= 1 && number <5 )
    Console.Write("Enter Number 1: ");
    input = Console.ReadLine();
    numberOne = Convert.ToDouble(input);
    Console.Write("Enter Number 2: ");
    input = Console.ReadLine();
    numberTwo = Convert.ToDouble(input);


    //for (int i = 1; i < words.Length; i++)
    // {
    // Console.Write("Enter Number {0} : ", i);
    // words[i] = Console.ReadLine();

    //if (words[0] =)
    //if (remember == 1)
    // Console.WriteLine(Add);



    } while (entryIsValid == true);

    public static double Add(double answer, double words)

    // answer = words[0] + words[1];

    return answer;


    By the way I'm still editing my method

  • Sign In to post your comments