0

I have the following code, what i do is the following: I enter a string of numbers with the following format and what I do is debug and adopt the correct format, example: correct format: XXXX/XX

456/12  = 0456/12  
25/1    = 0025/01
1/23    = 0001/23
/       = 0000/00

but what I don't take into account is that if what enters is an intger or not, if it is an integer it accepts it but it rejects it. for example:

A324/1   =  FORMAT ERROR
458/P8   = FORMAT ERROR

How to solve this problem?

my code:

 public static string Validate_Cc(string CourtCase)
        {
           // int i = 0;
            string[] parts = CourtCase.Split('/');
            var number1 = Int32.Parse(parts[0]);
            var number2 = Int32.Parse(parts[1]);

            if ((CourtCase.Length) > 7)
            {
                 badLines(CourtCase);
            }
         
             return $"{number1:0000}/{number2:00}";
        }
  • 4
    Does this answer your question? [C# testing to see if a string is an integer?](https://stackoverflow.com/questions/1752499/c-sharp-testing-to-see-if-a-string-is-an-integer) – kosist Oct 29 '21 at 09:06

3 Answers3

2

Use int.TryParse() instead of Int32.Parse

Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.

//I updated condition as well as variable names as per the .net naming convensions
public static string Validate_Cc(string courtCase)
{
    string[] parts = courtCase.Split('/')
           .Select(x => string.IsNullOrEmpty(x) ? "0" : x) //To handle "/" edge case
           .ToArray();

    if(int.TryParse(parts[0], out int number1) && int.TryParse(parts[1], out int number2))
        return $"{number1:0000}/{number2:00}";          
    else
        return BadLines(courtCase);
}

Try online

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
0

Use this code:

public static string Validate_Cc(string CourtCase)
    {
    string[] parts = CourtCase.Split('/');
    if(parts.Length != 2)
    {
        badLines(CourtCase);
    }
    for(int i = 0; i < parts.Length; i++)
    {
        if(parts[i] == "")
        {
            parts[i] = "0";
        }
    }
    var isValidInput = int.TryParse(parts[0], out int number1) & int.TryParse(parts[1], out int number2);
    if(!isValidInput) 
    {
        badLines(CourtCase);
        return input;
    }
   
    return $"{number1:0000}/{number2:00}";
    }

First, you check whether there is an / in your string by checking the length of parts. Then you replace empty string with "0". Make sure that the second TryParse is executed by using the & operator.

SomeBody
  • 7,515
  • 2
  • 17
  • 33
  • thanks for your support, i have run your code and it works fine but there is a small nuance: for example, `A45/25` should not be set to `0000/25`. i should just ignore it and report it in the `badLines()`.. for other cases that are numeric: `25/02 -> 0025/02` it would be correct to apply format. – Juan Sastre Oct 29 '21 at 10:30
  • @JuanSastre But your method has to return something. What do you want to return if it is an invalid input? – SomeBody Oct 29 '21 at 10:42
  • if it's an invalid entry, i return that input value in a csv file which is the `badLines(string baditem)`, this method generates a csv with those invalid entries so it's easier to locate them and correct their format based on other parameters. – Juan Sastre Oct 29 '21 at 10:55
  • i mean that i return the same value: `A234/3 -> A234/3` or `AP1/3 -> AP1/3` ... return the same value, but reports the issue that it is not an integer string via `badLines()` – Juan Sastre Oct 29 '21 at 10:57
  • @JuanSastre This can be done quite easily. Just add `return input` inside the if clause after calling `badLines`. I edited my answer. – SomeBody Oct 29 '21 at 11:31
0

I suggest checking pattern with regex and then parsing:

private static string ParseCourtCase(string courtCase)
{
    if(!Regex.Match(courtCase, @"\d+\/\d+").Success)
    {
        throw new InvalidOperationException("Incorrect format of input string.");
    }

    var parts = courtCase.Split('/');
    var number1 = int.Parse(parts[0]);
    var number2 = int.Parse(parts[1]);

    return $"{number1:0000}/{number2:00}";
}
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69