Solved

i need your help..i have two questions

Posted on 2002-06-02
11
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java program running SQL query 5 48
web project error add remove 1 52
What is the use of Forwarding Class in java 1 37
Html split(text) 2 31
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

733 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