-1

What i'd like to do is that to check if the user input fits and if so then use the user input, otherwise throw exception.

For fit there can only be one true input which is in form of: x,y;x,y;x,y;x,y where x >= 0 & y > 0. (x and y do not have to be same value in every comma separator, for example true input is 0,1;2,3;4,5;6,7) If user types anything else, for example "asd" or 0,1;2,3;4,5 (missing one x,y), he gets the exception. I think i can handle doing the exception throwing but the problem is i don't know how to check if the user input fits. I don't know if it's necessary to provide my code here because the checking part is not in my code yet and anything else is unimportant anyways.

Code was requested, don't know for what reasons but created some for quick example:

TextField tf1 = new TextField();
String inputText = tf1.getText();
if (inputText == form(x,y;x,y;x,y;x,y)) {
    // do things;
}
else {
    throw exception;
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
charen
  • 371
  • 1
  • 7
  • 20

2 Answers2

0

Use regexp

if (inputText.matches("[0-9]+,[1-9]+[0-9]*(?:;[0-9]+,[1-9]+[0-9]*){3}"))
{
    // do things
}    
ToYonos
  • 16,469
  • 2
  • 54
  • 70
0

From your test case 0,1;2,3;4,5, it is defined that a number of yours ("x") would consist of either zero of a sequence of integers not trended by zero. This would be:

(?:0|[1-9]\d*+)

From there you can quantify a subpattern for repeating the rest of the section "x,y":

(?:0|[1-9]\d*+),[1-9]\d*+(?:;(?:0|[1-9]\d*+),[1-9]\d*+){3}

Here is a regex demo.

Unihedron
  • 10,902
  • 13
  • 62
  • 72