Link to home
Start Free TrialLog in
Avatar of miss_oracle
miss_oracle

asked on

i need your help..i have two questions

i really need your help in getting a solution for two problems i am facing..

1) i have a program where the user enters the
    Day e.g Saturday
    the data type of the day is String.. i want to add a validation as the probram should accpet only characters not integer on this variable so how can i do it..

2) the second question is i want the user to enter the day to continue to press on Enter key to quit.. how can the prgram understand the Enter key for quitting..

i am really waiting for your answers.. please..
ASKER CERTIFIED SOLUTION
Avatar of Peter Kwan
Peter Kwan
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of s_lavie
s_lavie

1)
Extend javax.swing.text.PlainDocument and overwrite the method:
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException
{
  // make your validations here
  super.insertString(offset, str, attr);
}
Set this document to your JTextField.

2)
Use addKeyListener to your JTextField.
Hello miss_oracle,

Are you using command-line or GUI components?
Good question pkwan!
Whatever you are using you should use something like

for (int i=0; i<dayString.length(); i++)
{
  if (!Character.isLetter(dayString.charAt(i)))
  {
    // then its not all characters, so throw an exception
    // or return false from this method or something
  }


}

// if the code gets to here then the word is OK.



Cheers,
Steve.
Steve,
I don't agree that your solution should be used whatever miss_oracle is using.
It would be fair enough if you posted your answer as a comment...
I think miss_oracle should reject your answer and let other experts provide their solutions as well.
Avatar of Mick Barry
I agree. Steve, please read the guidlelines for proposing answers.
Well, i think my input was more useful than either of your two comments (particularly objects)? Do they help anybody ?  At least s_lavie has offered a suggestion. After all all I am doing is offering a solution to her problem, most people consider this to be an answer, she doesn't have to accept it.
miss_oracle,

The answer to your question depends largely on whether you are using a command line or a GUI interface.

1.

If you are using a command line interface then I don't think there is a way to check individual characters as they are type because standard input is buffered. And you can only check if the returned string is valid as suggested by pkwan.

If you are using a GUI then a custom Document can be used to validate the input as it is being entered as suggested by s_lavie. This allows you full control over restricting input.  Another possible solution if you have a predefined set of valid inputs would be to use combo box offering the available options instead of relying on the user to enter them.

2.

To check for enter being returned just check for a zero length string as suggested by pkwan.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points to pkwan

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
Comment from expert accepted as answer

Computer101
E-E Admin