• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Validate and Integer from 0 - 9?  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am very new to programming but eager to learn. I created a GUI with a jTextField as an input box and am wondering how to validate that this data is an integer from 0 - 9.
Here is what I have. However the if statement shows an error that says int cannot be dereferenced. Any help?


 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall move you to a more general forum since that isn't a specific Java8 problem. You might still stand a chance of winning the book I shall also add code tags to your post; always use them. You will be able to see how much better they look.

You can't find a method which takes a range like that, or more precisely I can't think of one just at the moment. Actually if you write 0-9 the compiler will see that as the expression zero minus nine and simply enter −9 for you.
  • One possibility: You can see whether the int is within a specific range. You need to check for both ends of the range and conjoin both tests with the AND operator.
  • Another possibility: Use a regular expression to check whether the text in the text box matches a single digit. The Java Tutorials has a good section about regular expressions. They can be very complicated, but checking 1 digit isn't too difficult. That also allows you to throw error messages before you get the NumberFormatException.
  •  
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't set the text to null. Set it to something like "".
     
    Bartender
    Posts: 689
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The variable 'category' is an int. This is a primitive value, not a reference to an object. When the compiler says that int cannot be dereferenced it means you cannot call methods on it.

    String has a matches(...) method, which is what I assume you were trying to call. This method takes a String with a regular expression in it and tells you whether the String matches the regular expression.

    However if you turn the string into an int you will need to do a numerical check as Campbell suggests.
     
    Sheriff
    Posts: 22845
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In real life you wouldn't do anything like that. The easiest way, I think, is to use a JFormattedTextField. Here's a link to Oracle's tutorial about that: How to Use Formatted Text Fields.
     
    Java Cowboy
    Sheriff
    Posts: 16060
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why not simply:

     
    Jordan Ruetz
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alright, so I have successful managed to change the code so that the message displays when a user enters a number that is not between 0 and 9. But how about when they click the button without enter anything at all? The message does not work then. How can I change the code so that it does?

    Here is what I have now:
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Did you find out anything about using a regular expression for a single digit/0? Did you find out about formatted text fields?
     
    Jordan Ruetz
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry, I've got it now. Thank you for your help!
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well done Do show us what your final solution was.
     
    Jordan Ruetz
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's probably not the best solution, but I did something like this:

     
    Ranch Hand
    Posts: 135
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How about this? It much less code.

     
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    In the code above:

    Line 4: You really don't need to use a regular expression here; the equals() method will do the job. That is, instead of input.matches(""), you can do "".equals(input) which also checks if input is an empty string. It may look strange but since the empty String literal "" is itself an object, you can call its equals() method. This way, you can avoid ever getting a NullPointerException (NPE) which you can get if you do input.equals("") and input is null.

    Lines 8-10: You have an empty else clause. The else clause is an optional part of the if-statement so since you aren't doing anything in the else part, you might as well leave it out.

    Line 11: This line will execute even if input has been determined to be empty -- that is, even when "".equals(input) is true, line 11 will still execute and you will get a NumberFormatException. That's probably not what you want.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!