Suppose there is an object to create which you need to get some data from the user (name, age, etc.). Obviously, before creating an object, you need to make sure that all data is correct (no empty lines, numbers in the correct range, etc.).

So, I still get to write something monstrous like:

if(name != null && !name.isEmpty() && age > 10 && age < 100 && (symbol == 'x' || symbol == 'o')) { //создание объекта } 

You can make methods to test and reduce:

 if(isNameValid() && isAgeValid() && isSymbolValid()) { //создание объекта } 

But all the same, it goes a long way, because there are only 3 parameters here, but in fact there can be more.

  1. So how to handle these situations correctly?
  2. And another such moment is that if the user entered the 5/6 data correctly, it is necessary not to ask from the beginning, but only to ask again where the error is. How to handle this? Store in the array the results of valid methods? And then choose from the array those that are wrong and ask for them or are there other options?

    1 answer 1

    Java has a rather useful exception mechanism that can help you with this:

     import java.util.StringJoiner; public class TestMain { private final String name; private final int age; private final char symbol; public TestMain(String name, int age, char symbol) { this.name = name; this.age = age; this.symbol = symbol; checkData(); } private void checkData() { StringJoiner errors = new StringJoiner("\n"); if (name == null || name.isEmpty()) errors.add("Имя не удовлетворяет условиям: " + name); if (age < 10 || age > 100) errors.add("Возраст не удовлетворяет условиям:" + age); if (symbol != 'x' && symbol != 'o') errors.add("Символ не удовлетворяет условиям: " + symbol); if (errors.length() > 0) throw new IllegalArgumentException(errors.toString()); } public String getName() { return name; } public int getAge() { return age; } public char getSymbol() { return symbol; } ... } 

    If you need to check local variables, then choose one of two ways:

    1) Process all data in one method:

     private boolean isDataValid(String name, int age, char symbol) { StringJoiner errors = new StringJoiner("\n"); if (name == null || name.isEmpty()) errors.add("Имя не удовлетворяет условиям: " + name); if (age < 10 || age > 100) errors.add("Возраст не удовлетворяет условиям:" + age); if (symbol != 'x' && symbol != 'o') errors.add("Символ не удовлетворяет условиям: " + symbol); if (errors.length() > 0) throw new IllegalArgumentException(errors.toString()); return true; } 

    2) Verify each parameter in a separate method (use this approach to avoid cumbersome constructions in one method, remember that methods should be easily readable, and if a method contains a large amount of code, its readability decreases):

     private boolean isDataValid(String name, int age, char symbol) { StringJoiner errors = new StringJoiner("\n"); if (!isNameValid(name)) errors.add("Имя не удовлетворяет условиям: " + name); if (!isAgeValid(age)) errors.add("Возраст не удовлетворяет условиям:" + age); if (!isSymbolValid(symbol)) errors.add("Символ не удовлетворяет условиям: " + symbol); if (errors.length() > 0) throw new IllegalArgumentException(errors.toString()); return true; } private boolean isNameValid(String name) { return name != null && !name.isEmpty(); } private boolean isAgeValid(int age) { return age >= 10 && age <= 100; } private boolean isSymbolValid(char symbol) { return symbol == 'x' || symbol == 'o'; } 

    Regarding clause 2: In practice, I would advise you precisely it, since the method should only do its work, it should not go into the logic of other methods, on which it, in fact, does not depend.

    • Yes thank you. Conveniently. All I realized that I was stupid)) Now it’s pretty clear. Unless a question what to do if in the designer / method there are a lot of parameters, or it is not terrible? - dimads
    • StringJoiner Only in jdk8, in version 7, I understood it not. - dimads 7:09 pm
    • @dimads, StringBuilder is. - Nofate
    • @dimads Updated the answer. Yes, StringJoiner appeared in Java 8, in Java 7 you have to use StringBuilder, but I would advise upgrading to version 8, since many things in it are much simpler and the code can be written much simpler and more readable (especially with streaming) . And to do a lot of parameters in the constructor / method is really scary, because the thinner the method, the better, I wrote about this in the answer. - PloadyFree