Java validating strings for digits


This was my first online interview after a long long time, so I took way too much time in this question. Now the algorithm boils down to 2 helper methods, boolean is Number(String s) { int start = 0; int digits = 0; int end = 0; start = parse_optional_character('-', s, end); end = parse_numerical_sequence(s, i, start); digits += end - start; start = parse_optional_character('.', s, end); end = parse_numerical_sequence(s, i, start); digits += end - start; return ((end == s.length()) && (digits @janos already gave you great advise about unit tests and using regex, but assuming that you cannot use them in an interview, here are some more comments: Long variable names I really like descriptive variable names, and yours are quite clear. Extract strings to static fields It's always good to extract repeating constants to fields.I want to gather feedback about my solution and better ideas for the solution to this problem. In this case it would be especially good for the decimal point, as a lot of languages do not use if (current Char == '-') { if (is Negative Found Already


For the sake of simplicity all the examples below feature field constraints and all validation is triggered by the controller.

Refer to the Bean Validation documentation for a full list of examples.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>