Solved

i need your help..i have two questions

Posted on 2002-06-02
11
219 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoresSpecial  challenge 13 40
changePi Challenge 15 75
java set up 1 46
Java DateChooser? 2 5
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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

12 Experts available now in Live!

Get 1:1 Help Now