0

I have written the following method to validate an input String and output it as an int array. The method works completely as I need it to but I would like to add some extra validation to it so that it only allows integers and commas in the input so there are no errors.

An example correct input would be:

"7,23,62,8,1130"

The method is:

public static int[] validator (String [] check) {

    int [] out = new int[5];

    try
    {
        if (0 < Integer.parseInt(check[0]) && Integer.parseInt(check[0]) < 100)
        {
            out[0] = Integer.parseInt(check[0]);
        }
        else
        {
            throw new InvalidMessageException();
        }
    }
    catch (InvalidMessageException ex)
    {
        System.err.println("Invalid instruction message");
        return null;
    }

    try
    {
        if (0 < Integer.parseInt(check[1]))
        {
            out[1] = Integer.parseInt(check[1]);
        }
        else
        {
            throw new InvalidMessageException();
        }
    }
    catch (InvalidMessageException ex)
    {
        System.err.println("Invalid instruction message");
        return null;
    }

    try
    {
        if(0 < Integer.parseInt(check[2]))
        {
            out[2] = Integer.parseInt(check[2]);
        }
        else
        {
            throw new InvalidMessageException();
        }
    }
    catch (InvalidMessageException ex)
    {
        System.err.println("Invalid instruction message");
        return null;
    }

    try
    {
        if (0 <= Integer.parseInt(check[3]) && Integer.parseInt(check[3]) < 256)
        {
            out[3] = Integer.parseInt(check[3]);
        }
        else
        {
            throw new InvalidMessageException();
        }
    }
    catch (InvalidMessageException ex)
    {
        System.err.println("Invalid instruction message");
        return null;
    }

    try
    {
        if(0 < Integer.parseInt(check[4]))
        {
            out[4] = Integer.parseInt(check[4]);
        }
        else
        {
            throw new InvalidMessageException();
        }
    }
    catch (InvalidMessageException ex)
    {
        System.err.println("Invalid instruction message");
        return null;
    }

    return out;

}

I have considered doing something like:

    inputText = inputText.replace(".", "");
    inputText = inputText.replace(":", "");
    inputText = inputText.replace(";", "");
    inputText = inputText.replace("\"", "");

etc... but it does not seem a particularly great solution. If anyone has a better idea, please let me know. Thanks very much for any help!

4
  • 3
    What about using Regular Expression ? Something like this ^(\d,)*\d$ would suits Commented Apr 8, 2016 at 14:49
  • I didn't fully understand what you want to do, Do you want to convert those values to int from a String input ? Commented Apr 8, 2016 at 14:49
  • @Walfrat ^(\d+,)*\d+$ is probably more accurate Commented Apr 8, 2016 at 14:51
  • @pablochan woops yes pablochan thanks Commented Apr 11, 2016 at 6:58

3 Answers 3

2

I'd say something like this should replace your method, without having read your code, just your requirements:

String input = "7,23,62,8,1130";
if (input.matches("(?:\\d+(?:,|$))+")) {
    int[] result = Arrays.stream(input.split(",")).mapToInt(Integer::parseInt).toArray();
} else {
    throw new InvalidMessageException("");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is what I was looking for.
2

You can use a regex expression to validate your input:

[0-9]+(,[0-9]+)*,?

Check it with the String matches(regex) method as:

if (yourString.matches("[0-9]+(,[0-9]+)*,?")) {

}

Comments

0

This is exactly what regular expressions are for:

return inputText.matches("\\d+(,\\d+)*");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.