EntomberExarch
EntomberExarch

Reputation: 17

How can I check if a string follows the pattern of american currency?

I'm working on a problem that wants me to get a string input from a user, run it through a method which will check every character to see if it follows the pattern of American currency. It has to be a string that goes into the method. the amount can be any where from 1 dollar to a thousand but must have the format entered as $x.xx, $xx.xx, $xxx.xx, as long as the user enters an amount that is consistent with the above formats then my program should output that its "valid" anything else would be a "invalid format" output. first character must be the '$' and I cannot use regex.

I get the user input and then validate it with .NullOrWhiteSpace. and then send the string value holding the user input down to my created method. from this point I have no idea how to continue. I've tried .ToCharArray, I have also tried making a long and complicated if statement and I have researched for a few hours now but can't find a solid way to write this out.

 class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("enter amount between $1.00 and $1000.00");
        string valueUS = Console.ReadLine();
        while (string.IsNullOrWhiteSpace(valueUS))
        {
            Console.WriteLine("Please enter in an amount");
            valueUS = Console.ReadLine();
        }

        currencyChecker(valueUS);

    }

    public static string currencyChecker(string currencyString)
    {

        char[] currencyArray;
        currencyArray = currencyString.ToCharArray();
        for (int i = 0; i < currencyArray.Length; i++)
        {

            if (currencyArray[0] == '$')
            {

            }

        }
        return currencyString;

the method below should check every character entered by the user and verify that it matches the above described pattern for American currency and output that its "valid" anything else should be reported back as "invalid"

Upvotes: 1

Views: 414

Answers (1)

Zohar Peled
Zohar Peled

Reputation: 82504

Usually, you would use a regular expression for something like this. A simple regex for this would be ^\$\d+.\d\d$. Basically, it means the string should start with a $ sign, have at last one digit, a dot, and two more digits.

However, this can be done without regular expressions, and since it seems like a homework task, I'll give you a nudge in the right direction.

So you need to test the string starts with $, the char 3rd from the right is a ., and everything else are digits.

Your method should return a bool indicating valid / invalid results - so you should do something like this:

 static bool IsCurrency(string currency)
 {

     // Check if the string is not null or empty - if not, return false

     // check if the string is at least 5 chars long, since you need  at least $x.xx - if not, return false

     // Check if the first char is $ - if not, return false

     // Check if the 3rd char from the end is . - if not, return false

     // check if all the other chars are digits - if not, return false


     // If all checks are valid -
     return true;
 }

Note that the order of the tests is critical, for instance if you check the 3rd digit from the right is a . before you check you have at least 5 digits, you might attempt to check a string that is only 2 digits long and get an exception.

Since this is (probably) homework I'm going to leave the code-writing part for you, so you would actually learn something from this.

Upvotes: 5

Related Questions