• Post Reply Bookmark Topic Watch Topic
  • New Topic

char string validation  RSS feed

 
Allen Watts
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
This is my first time posting here, I can not seem to find resources that I can apply in order to make this work!
I need to validate a 6 character string that is user input. 
The first 2 characters HAVE to be IT, either upper or lower case and the last 4 characters can be any numbers...  If the user inputs anything else, like for example TP2345 I need to print that the input was invalid.
I have read a lot about other types of validation, but I can not seem to apply them to this char inside of a string application...

output should look like this...

Enter a code for validation (e.g. IT8877):  IT2258
Code: IT2258 is valid.

or

Enter a code for validation (e.g. IT8877):  hu47hu
Code: hu47hu is not valid
First character is not an I or i
Second character is not a T or t
Fifth character is not a digit
Sixth character is not a digit

Any advice or sample code would be very much appreciated!!!

Thank you,
Allen

 
 
Vishakha Lele
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, java regex will help in this case. You can use following code which uses a java patterns with regular expression. Following is a sample code.

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
which appears in the heading of this forum.We believe it doesn't help questioners to give them complete solutions straight off; it is better to guide them. Please don't be annoyed with me, but I have pulled rank and removed your complete solution.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think the bit about regular expressions is a good suggestion; you can read more about regular expressions in the Java™ Tutorials.
There are other ways to do it. The String class has many methods some of which allow you to get an individual letter, e.g. the char at position 999. (Remember the first position is 0, not 1.)
Once you have that char, you can't do that much with it on its own, but you can test for equality to other chars. You can also test for ≥ or ≤ other chars, because a char is stored as a (non‑negative) number, not a letter.
There are also methods in the Character class which do all sorts of things with a char, including changing case and testing whether a char is a particular type.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishakha Lele wrote:Actually, java regex will help in this case. You can use following code which uses a java patterns with regular expression.


Actually, I don't see how regexes would work here (as needed). Take a look at the output. It has full descriptions of each mismatch.

The OP needs to code the output, and with regexes, it either matches or it doesn't. There isn't a response for why it matches, nor is there a response for continuing to explain why it doesn't match after it already doesn't match.

Henry
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How much detail do you need to give?

For example, if the length of the code is not 6 characters do you still need to go through the rest of the validation?

So, would 'hu47' produce:
Code: hu47 is not valid
Too short, not 6 characters (or some equivalent message)
First character is not an I or i
Second character is not a T or t
Fifth character is not a digit
Sixth character is not a digit

or:
Code: hu47 is not valid
Too short, not 6 characters (or some equivalent message)
First character is not an I or i
Second character is not a T or t

or even:
Code: hu47 is not valid
Too short, not 6 characters (or some equivalent message)

The rules for the validation output will need to be quite well defined.
 
Allen Watts
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
Thank you for the interest in my post!

Any code that was entered UNDER the 6 digits would not be considered valid. 

Code: hu47 is not valid
Too short, not 6 characters (or some equivalent message)

would work perfectly. 

I have considered reading string input with IndexOf and then somehow validating each character in the string that way, but I can't figure out how to do it- it is the only way I can think of in order to code the output in the required way...

Thanks,
Allen
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for noticing
OP: please look at this; people need to know about the double post both here and on dreamingcode.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!