1

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"

  • 1
    [The answers to this question](https://stackoverflow.com/questions/354044/what-is-the-best-u-s-currency-regex) show a good solution that will do the job nicely (assuming of course this isn't a homework assignment, in which case you'll almost certainly be failed for cheating with regex). – Mark Feldman Jan 27 '19 at 11:28
  • Yes, unfortunately I cannot use regex, I'm not use if I should even use the .ToCharArray I think I may be over complicating it with that. – EntomberExarch Jan 27 '19 at 11:32

1 Answers1

5

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.

Zohar Peled
  • 73,407
  • 8
  • 53
  • 101
  • I really appreciate the guidance and I'm pretty sure I can solve the problem with this info, its was out of what I was thinking to do, this stuff is challenging. – EntomberExarch Jan 27 '19 at 11:45
  • Everything is hard at the beginning. Glad to help :-) – Zohar Peled Jan 27 '19 at 11:46
  • You could change the check for at least five chars long to a check if string is *exactly* between 5 to 7 chars long otherwise return false – Fabjan Jan 27 '19 at 11:48
  • @Fabjan Yes, if there is an upper limit it can also be checked by checking the length of the input string, but if the upper limit is $9999.99 you need 8 chars, not 7. – Zohar Peled Jan 27 '19 at 11:53
  • @ZoharPeled Yes, the OP specified that "(input) can be anywhere from 1 dollar to a thousand"... – Fabjan Jan 27 '19 at 11:54
  • @ZoharPeled It's just a side note... Your answer is very good otherwise – Fabjan Jan 27 '19 at 12:01
  • @ZoharPeled, sorry to bother you guys again but I managed to get everything working except for how to check if all the other Chars are digits. all my tests are passing except one with a letter right in the middle of it. – EntomberExarch Jan 27 '19 at 12:49
  • Loop from 1 to length-1, Use [`Char.IsDigit`](https://docs.microsoft.com/en-us/dotnet/api/system.char.isdigit?view=netframework-4.7.2), but make sure you are not testing the 3rd from end char which is a dot. – Zohar Peled Jan 27 '19 at 12:51