Solved

i need your help..i have two questions

Posted on 2002-06-02
11
230 Views
Last Modified: 2010-03-31
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..
0
Comment
Question by:miss_oracle
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 300 total points
ID: 7049309
1) You may try to use:

if (date_string.equals("Sunday")) { // or equalsIgnoreCase if you want to ignore the case of input
   // your sunday code
}
else if ( /* same for monday  */ )
...

2) You may try this one:
try {
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
String input_str = b.readLine();
if (input_str.length() == 0) { // just pressing enter
   System.exit(0);
}
} catch (Exception e) { // error handling code
}

Please modify the codes yourself in order to fit your need.
0
 
LVL 2

Expert Comment

by:s_lavie
ID: 7049352
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.
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 7049357
Hello miss_oracle,

Are you using command-line or GUI components?
0
 
LVL 2

Expert Comment

by:s_lavie
ID: 7049359
Good question pkwan!
0
 

Expert Comment

by:StevenWood
ID: 7066828
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:s_lavie
ID: 7072604
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 7081065
I agree. Steve, please read the guidlelines for proposing answers.
0
 

Expert Comment

by:StevenWood
ID: 7083279
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 7084581
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.
0
 
LVL 5

Expert Comment

by:vemul
ID: 7888100
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
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7959673
Comment from expert accepted as answer

Computer101
E-E Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now