@Test void testSingleSentence() { NumberFormatValidator validator = new NumberFormatValidator(); Sentence st0 = new Sentence("1, 10, 200, 1,000.00, and 10,000 and 10,000.00 and 100,000.00 and 10,000,000.0 should all be fine.", 0); Sentence st1 = new Sentence("10000 gives an error because it should really be formatted as 10,000.", 0); Sentence st2 = new Sentence("100,00 has a comma in the wrong position but 100.00 does not.", 0); Sentence st3 = new Sentence("10,0000,000 also has a comma in the wrong position.", 0); Sentence st4 = new Sentence("10000.0.0 has too many decimal places and requires a delimiter.", 0); Sentence st5 = new Sentence("100,00,000.0 has a sequence of digits that is too short.", 0); Sentence st6 = new Sentence("Years are ignored by default, like 1995 or 2016", 0); List<ValidationError> errors = new ArrayList<>(); validator.setErrorList(errors); validator.validate(st0); validator.validate(st1); validator.validate(st2); validator.validate(st3); validator.validate(st4); validator.validate(st5); validator.validate(st6); assertEquals(6, errors.size(), st1.toString()); }
@Override public void validate(Sentence sentence) { String text = sentence.getContent(); String number = ""; boolean haveNumber = false; int startPosition = 0; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if ((i == text.length() - 1) && (DOT_DELIMITERS.indexOf(ch) != -1)) { break; } if (Character.isDigit(ch) || (haveNumber && ((COMMA_DELIMITERS.indexOf(ch) != -1) || (DOT_DELIMITERS.indexOf(ch) != -1)))) { number += text.charAt(i); if (!haveNumber) { startPosition = i; } haveNumber = true; } else { validateNumber(sentence, startPosition, number); number = ""; haveNumber = false; } } validateNumber(sentence, startPosition, number); }
NumberFormatValidator extends Validator { @Override public void validate(Sentence sentence) { String text = sentence.getContent(); String number = ""; boolean haveNumber = false; int startPosition = 0; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if ((i == text.length() - 1) && (DOT_DELIMITERS.indexOf(ch) != -1)) { break; } if (Character.isDigit(ch) || (haveNumber && ((COMMA_DELIMITERS.indexOf(ch) != -1) || (DOT_DELIMITERS.indexOf(ch) != -1)))) { number += text.charAt(i); if (!haveNumber) { startPosition = i; } haveNumber = true; } else { validateNumber(sentence, startPosition, number); number = ""; haveNumber = false; } } validateNumber(sentence, startPosition, number); } }
NumberFormatValidator extends Validator { @Override public void validate(Sentence sentence) { String text = sentence.getContent(); String number = ""; boolean haveNumber = false; int startPosition = 0; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if ((i == text.length() - 1) && (DOT_DELIMITERS.indexOf(ch) != -1)) { break; } if (Character.isDigit(ch) || (haveNumber && ((COMMA_DELIMITERS.indexOf(ch) != -1) || (DOT_DELIMITERS.indexOf(ch) != -1)))) { number += text.charAt(i); if (!haveNumber) { startPosition = i; } haveNumber = true; } else { validateNumber(sentence, startPosition, number); number = ""; haveNumber = false; } } validateNumber(sentence, startPosition, number); } NumberFormatValidator(); }
NumberFormatValidator extends Validator { @Override public void validate(Sentence sentence) { String text = sentence.getContent(); String number = ""; boolean haveNumber = false; int startPosition = 0; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if ((i == text.length() - 1) && (DOT_DELIMITERS.indexOf(ch) != -1)) { break; } if (Character.isDigit(ch) || (haveNumber && ((COMMA_DELIMITERS.indexOf(ch) != -1) || (DOT_DELIMITERS.indexOf(ch) != -1)))) { number += text.charAt(i); if (!haveNumber) { startPosition = i; } haveNumber = true; } else { validateNumber(sentence, startPosition, number); number = ""; haveNumber = false; } } validateNumber(sentence, startPosition, number); } NumberFormatValidator(); @Override void validate(Sentence sentence); }
NumberFormatValidator extends Validator { @Override public void validate(Sentence sentence) { String text = sentence.getContent(); String number = ""; boolean haveNumber = false; int startPosition = 0; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if ((i == text.length() - 1) && (DOT_DELIMITERS.indexOf(ch) != -1)) { break; } if (Character.isDigit(ch) || (haveNumber && ((COMMA_DELIMITERS.indexOf(ch) != -1) || (DOT_DELIMITERS.indexOf(ch) != -1)))) { number += text.charAt(i); if (!haveNumber) { startPosition = i; } haveNumber = true; } else { validateNumber(sentence, startPosition, number); number = ""; haveNumber = false; } } validateNumber(sentence, startPosition, number); } NumberFormatValidator(); @Override void validate(Sentence sentence);  }