Skip to main content
added 520 characters in body
Source Link
Marc-Andre
  • 6.7k
  • 5
  • 38
  • 65

Don't treat invalid input by replacing it to a default value, unless it's meaningful to do so. In case of teleportation, I would not consider it valid to be teleported to 0,0 when I'm entering invalid positions.

I would treat an invalid value by returning a message next to the textfield saying that the value is not in the range or format expected. Remember to provide an example of an expected value, this really helps user providing good information. You should check both input before returning any error message, I think users prefers to tackle more than one error, that to fix one and be stop because it did not verify the other one too.

After a bit of research, you could use a TextFieldFilter, to filter out invalid input. One advantage of this approach is it make less prone to have invalid input, what you need to make sure is that it's not just a client-side validation that could be potentially de-activated by the user. If it so, it would not change the code since you would need to valid it server-side too.


try {
        position = Integer.parseInt(string);
    } catch (Exception e) {
        System.out.println("Invalid input");
        position = 0;
    }

I find it a bad habit to output to Sysout when you manage an exception. Not all environments have a console ready to output, use a logger instead. It can configure by each of your environment and it's easier when you want to debug something. The second thing here is that your catching Exception blindly and not what you're looking for. Integer.parseint() can throw a NumberFormatException if the string does not contain a parsable integer, so catch that instead. Now it's clear that you only what to validate input.

Don't treat invalid input by replacing it to a default value, unless it's meaningful to do so. In case of teleportation, I would not consider it valid to be teleported to 0,0 when I'm entering invalid positions.

I would treat an invalid value by returning a message next to the textfield saying that the value is not in the range or format expected. Remember to provide an example of an expected value, this really helps user providing good information. You should check both input before returning any error message, I think users prefers to tackle more than one error, that to fix one and be stop because it did not verify the other one too.


try {
        position = Integer.parseInt(string);
    } catch (Exception e) {
        System.out.println("Invalid input");
        position = 0;
    }

I find it a bad habit to output to Sysout when you manage an exception. Not all environments have a console ready to output, use a logger instead. It can configure by each of your environment and it's easier when you want to debug something. The second thing here is that your catching Exception blindly and not what you're looking for. Integer.parseint() can throw a NumberFormatException if the string does not contain a parsable integer, so catch that instead. Now it's clear that you only what to validate input.

Don't treat invalid input by replacing it to a default value, unless it's meaningful to do so. In case of teleportation, I would not consider it valid to be teleported to 0,0 when I'm entering invalid positions.

I would treat an invalid value by returning a message next to the textfield saying that the value is not in the range or format expected. Remember to provide an example of an expected value, this really helps user providing good information. You should check both input before returning any error message, I think users prefers to tackle more than one error, that to fix one and be stop because it did not verify the other one too.

After a bit of research, you could use a TextFieldFilter, to filter out invalid input. One advantage of this approach is it make less prone to have invalid input, what you need to make sure is that it's not just a client-side validation that could be potentially de-activated by the user. If it so, it would not change the code since you would need to valid it server-side too.


try {
        position = Integer.parseInt(string);
    } catch (Exception e) {
        System.out.println("Invalid input");
        position = 0;
    }

I find it a bad habit to output to Sysout when you manage an exception. Not all environments have a console ready to output, use a logger instead. It can configure by each of your environment and it's easier when you want to debug something. The second thing here is that your catching Exception blindly and not what you're looking for. Integer.parseint() can throw a NumberFormatException if the string does not contain a parsable integer, so catch that instead. Now it's clear that you only what to validate input.

Source Link
Marc-Andre
  • 6.7k
  • 5
  • 38
  • 65

Don't treat invalid input by replacing it to a default value, unless it's meaningful to do so. In case of teleportation, I would not consider it valid to be teleported to 0,0 when I'm entering invalid positions.

I would treat an invalid value by returning a message next to the textfield saying that the value is not in the range or format expected. Remember to provide an example of an expected value, this really helps user providing good information. You should check both input before returning any error message, I think users prefers to tackle more than one error, that to fix one and be stop because it did not verify the other one too.


try {
        position = Integer.parseInt(string);
    } catch (Exception e) {
        System.out.println("Invalid input");
        position = 0;
    }

I find it a bad habit to output to Sysout when you manage an exception. Not all environments have a console ready to output, use a logger instead. It can configure by each of your environment and it's easier when you want to debug something. The second thing here is that your catching Exception blindly and not what you're looking for. Integer.parseint() can throw a NumberFormatException if the string does not contain a parsable integer, so catch that instead. Now it's clear that you only what to validate input.