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}";
        }
1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

6 Comments

i use int.Parse(), when i use int.TryPase() it generates an error.
What is an error?
@JuanSastre, I updated my code to make it more clear, kindly take a look
thansk for your support, i have tried to execute your code but I get a syntax error in: return badLines(courtCase); because it cannot convert a void type to string
method badlines: public static void badLines (string baditem){....}
|
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}";
}

Comments

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.

5 Comments

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.
@JuanSastre But your method has to return something. What do you want to return if it is an invalid input?
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.
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()
@JuanSastre This can be done quite easily. Just add return input inside the if clause after calling badLines. I edited my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.