Solved

i need your help..i have two questions

Posted on 2002-06-02
11
247 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 77
Running JavaFX on JDeveloper 12C 1 55
Java: anonymous class 4 29
Crystal Reports Licensing Questions 4 35
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)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

829 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