I can't figure out the code for,

1) Get an error message dialog when there's no input in the textfield.
2) Get an error message when you put alphabets or other characters except for numbers.

try {
    /* get text from textfield and convert it to integer */
    //Integer.parseInt(txtbox.getText())
} catch(NumberFormatException e) {
    //show error message
}

Hi Fruit Punch,

Welcome to DaniWeb :).

Without any of your code as a starting point it is very difficult for us to give you a direct answer, but we prefer to give you the information you need to get your results anyway!

@cool_zephyr's approach will work, however it is slow (due to exceptions) and considered bad practise.

If you are having issues with working out how to display an error message, try looking at JOptionPane, if you still have difficult understanding how to use it post an example of your code and we can explain what you're doing wrong.

  1. If your issue is you are unsure how to check if the field is empty. Then consider the following code snippet.

    jTextField.getText().isEmpty()

  2. For determining if it is a number you could use functions such as Character.isDigit() to determine if the string contains all digits. Not this will only work for positive integers, you will need to be a little bit more complex if you want to account for negative values and floating point numbers.

Another option would be to consider regex, but it all really depends on your needs.

Chris

Freaky_Chris:
so, if you have a number, you are going to check each character one by one whether it's a digit ? (and this is only for the complete numbers).
Cool_zephyr's approach might not be the fastest, but considering the (very) little decrease in running speed, it's still a good one.
yes, indeed, it might be a bit slower, but then again, running isDigit on every single char of "12584588854545525545457454212564848452212474845424545<....>" might also take some time

Yes (and don't forget exactly 0 or 1 minus signs!).
Although in general Exceptions are not recommended for user error handling, this is one of those exceptions to the rule. Even Integer.valueOf(string) does it.

I undestand that there are exceptions to the rule. But it's not always the correct case to do either.

I think it comes down to a few things, firstly whether or not you are expecting a higher volume of passes, or fails. You would assume in his case it is normally going to be valid input, so this is an argument for try/catch.

But also the OPs original question does infact state any non numeric characters, which suggests that there will be no input such as (-|.) e.g. characters associated with negative values & floating point numbers.

Also, suggesting the comparison of a large number doesn't really play ball here. Integer.toString() is going to be limited by the value of Integer.MAX_VALUE so if you only wish to allow numbers upto that size then it will only have 10 digits to check (at max). On the flip side, if he is wishing to allow numbers to be greater than this, then there could well be meritt to manually checking. But this does depend on how the data is intended to be used, as Long, or BigDecimal may be a better approach.

But I understand that perhaps I am a little over the top inregards to things like this due to the fact I am used to developing on restrained equipment, where by avoid exception is seriously important, and I comply with the fact that an exception is only used in an exceptional case because of this.

Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.