This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
The first thing I want to say is that I’m brand new to java so with that in mind here is my problem.
I have a small app that basically generates a random string of characters that the user can use as a password. The length of the password can be defined by the user and here lies the problem. I want the input of the JTextfield to be restricted so that only numbers can be entered and that it’s limited to 3 characters. As it stands the code can do that, however it does not do it how I would like it to. The entire mask has to be filled in by the user, so for example the value 1 has to put in as 001 which is not what I want to be. Also it is possible to have gaps in between the numbers like 1 5 which causes it to error. I’ve removed it silently erroring by adding a workaround but all it does is that if that error happens it just defaults to 000.
i'll include class Genarate and class Copy just incase you want to run the entire app
I agree this is not directly related to you current problem, but if possible, try and consider a JSpinner with a SpinnerNumberModel instead of a text field. From the usability point of view, it would be more suitable.
thanks for the quick reply and yes i had conciddered using a JSpinner but thats not the effect i want for this.
i was conciddering puting in a second if command before the first to determin if it is a number being inputed and if not then it rejecting the input but im not sure how to tell the if command to look for somthing like that.
but i want only a numerical imput. can i use the DocumentFilter to do this and if so how?
You have to loop through all the characters in the input string to make sure they are numeric. Look at the Character class which has a method you can use to check for numeric digits.
Joined: Sep 01, 2009
Thanks for the reply Rob.
i had a gd look at Character Classes and i hope i've under stood it proppolly.
i made some changes to my code so that should let it work how i want it to,
The idea is that the code checks to see if the number inputed is a number, and if it is then checks to see if this would make the string too long. however every input (including numerical ones) throws the first error message, saying it is an ilegal character. i think i have in correctlly writen the syntex that checks the input but i can't get the program to compile any other way. if someone can point out were i've made the mistake (or if i have completely misunderstood what i should do) can you point it out please.
thanks in advance
Joined: Sep 01, 2009
sorry everyone i just noticed why it wasn't working. i was trying to use \b as the filter for numbers, but i should have been using \\d
thanks very much to everyone who helped me solve this problem