File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Validation problem please help?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Validation problem please help??" Watch "Validation problem please help??" New topic
Author

Validation problem please help??

atif abbas
Greenhorn

Joined: May 21, 2002
Posts: 7
Hi??
i have a problem in java...and i'm not quite sure how to solve it, and was wandering if anyone would be able to help me out here...
i have started something but not sure what to do next.
Here is the problem:
Problem 1
A program accepts identification number, first name, last name, and email address of a user. It validates the data as per following specifications:
.identification number – no blanks, four numerical characters;
.first name – no blanks, all alphabetical characters;
.last name – same as for first name;
.email address–
.no blanks;
.only one ‘@’character;
,there is at least one word before ‘@’, and at least two words after ‘@’, both words(following ‘@’ joined by ‘.’, no space between the words before and after the ‘.’ character.
Assume that there are maximum of two words following ‘@’character, and there are maximum of 25 characters in the email address.
If any of the data item is invalid, program displays an appropriate message to the user and asks for re-entry of that item.
When all data items entered are valid, program creates a password for the user by joining:
.first two characters of first name;
.2nd and 3rd characters of identification
.last three characters from the last name;
in above order.
Password is then relayed back to user. Later then enters this password and program then checks that user has entered the correct password (as created for him/her), and displays the message to the user accordingly.
You do not need to create any special user interface but must utilize string and array processing functions
and here is my source code so far.. if any one helpful enough could fix the program for me that would be much appreciated..thanks
kind regards,
Atif.
/******begining of code ****************|

/*****************end of code*******************/
[I've edited your post, surrounding the code with the proper ubb tags in order to preserve formatting and make the code easier to understand. -Dirk]
[ May 21, 2002: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
atif,
Thank you for registering with a valid JavaRanch display name.
Continuing this conversation from your previous post...
From my point of view, it would be much easier to help you figure out this problem, if we knew just a little bit about your comfort level with Java programming. Do you know how to get a single input from the user and display this input?
Have you tried my previous suggestion to "write a very simple program to just get one piece of information. Once you get that working, then try dealing with multiple attributes and the required manipulations."
Don't forget, the code you've posted won't compile. I've made note of a trouble area in a comment above.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
saab
Greenhorn

Joined: May 17, 2002
Posts: 4
hi dirk,
yes i know how to get a single input from the user..
for example if for the ID, i have to convert it to a string first like this..
<code>
id = Integer.parseInt(input.readLine());
</code>
then, ask user to input the string:
but, the trouble im having is how to validate the input by making sure there are no blanks, and only 4 numerical characters are entered...
then, get the firstname from the user and the lastname...
<code>
something like this: System.out.println("Enter Firstname:");
firstname = input.readLine();
//then validate here and check for no blanks and only alphabetical characters entered..
and same for the lastname..
</code>
Back to you dirk...
what do you think..can you help me some how ??
kind regards
Atif.
atif abbas
Greenhorn

Joined: May 21, 2002
Posts: 7
sorry. dirk i was using my old login name for the previous reply..
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Atif,
Look at the String method toCharArray(). It should help you along with some Character methods. For example, no blanks:

Another example all digits:

Regards,
Manfred.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
As David suggested, if you were familiar with regular expressions, then making use of the new regex package would be easiest. I haven't run across any great regex tutorials yet (nor have I looked very hard), though a decent one that explains the use of a (free) third party regex package can be found at javaregex.com.
Without the regex package, you'll just have to make liberal use of Manfred's suggestion and the various methods available in the String class.
As an example, the first entry must be four numerical characters long with no spaces. Before or after (I'd choose before) the input passes Manfred's two tests, make sure that the length of the input String ( String::length() ) is equal to four. And so on... If you're going to be doing a lot of this, then learning the regex package is definitely the way to go.
Are you getting any ideas?
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

some ideas on using the Character Class
Jamie
atif abbas
Greenhorn

Joined: May 21, 2002
Posts: 7
hey, guys many thanks for your help..
but i still get errors when i compile...
i have tried what you guys sais but not working..
Also how am i able to retrieve the password, once i have got the firstname, lastname and id..
if you read my question from the project it mentions that the password is created by the joining the firstname, id and lastname..
can you guys help me here again please...
here is my code so far..

[ May 22, 2002: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
OK, Let's discuss some of the errors in your example. I've used caps to bring emphasis to my comments.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
It would seem to me that you'd do well to start smaller and then build your larger program (as I've suggested).
Write a simple program to get just a String from the user, then display the gotten String to verify you know how to get a String and use it. Can you do this?
I'd guess, that with the ackward attempts at recursion in your various methods, you were trying to keep getting input from the user until the gotten input were in the proper format. Try a while loop instead. Something along these lines:

Good Luck.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validation problem please help??
 
Similar Threads
help with this program! I am stuck
Getting problem in struts
Hibernate 3: java.sql.SQLException: Field 'passwordConfirmation' doesn't have a default value
Need help with validation ?
OOP and user validation?