• Post Reply Bookmark Topic Watch Topic
  • New Topic

Testing Input From User  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I'm Trying to be able to validate on my code users Input such as AB123 or CD345 meaning the user can only enter the first two letters and the rest 3 numbers giving the proper errors, and obviously doing a do loop to ask the question again and this is what i have coded:

do{
//To ASk The User To Enter An Inventory Item Number
System.out.println("Hello Please Enter The Inventory Item Number");
itemNumber = readable.nextLine();

if (itemNumber.length() > 5)
{
System.out.println("**Error 01** - Item May Only Contain Five Characters");
if (itemNumber.charAt(0) != 'A' && itemNumber.charAt(0) != 'a' )
{
System.out.println("**Error 02** - Item Number Must Start With Two Letters");
}
As you can see first Im testign the user is entering 5 Letters nothing less nothing more.. second Im using a charAt() method to test that the Space zero its either from A to Z(yes Im thinking to write aaaall that line of code and im thinking there should be a shorter way than testing space zero and space 1)
Also i was thinking that perhaps with the substring method i could do it but I'm not able to code the Syntax
My questions would be :
-What is the best Approach to test this situation ?
- if the substring method to test is the shorter way (which is the only thing i can think of) what would be the proper syntax?
- do i also need to parse itemNumber = readable.nextLine(); to a double to be able to convert it to a return value to be usable?

thanks a lot for any kind help you guys can offer me i'd really apprecciate it
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it could be a lot easier using regex, something like:



I'm not very good at regex, so probably there's even a better solution.
 
Ranch Hand
Posts: 198
Java Linux Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Regular Expression is always the best choice to solve such kind of problems/validations.

However the other solution I can think about it to process each character in the String one by one.
take first character of the string and check if this in between A-Z, same for the second character.
for rest of the 3 characters either convert them first to integer to check with their value between 0-9 or you can compare them like X>='0' &&x<='9'.

If your Item no. exactly have 5 Characters then you have to put check that user is entered exactly 5 chars.

 
Juan Bonilla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi as you can see Im doing the loop to first be able to test the user input to be no loger or shorter than 5 letters and than making sure charAt (0) and charAt(1) are tested to be letters but they dont seem to be able to test charAT(1) im not sure why and the DO loop doesnt work any suggestions?

do{
//To ASk The User To Enter An Inventory Item Number
System.out.println("Hello Please Enter The Inventory Item Number");
itemNumber = readable.nextLine();
if (itemNumber.length() > 5)
{
System.out.println("**Error 01** - Item May Only Contain Five Characters");
}
if (itemNumber.charAt(0) != 'A' && itemNumber.charAt(0) != 'a' && itemNumber.charAt(0) != 'B' && itemNumber.charAt(0) != 'b'
&& itemNumber.charAt(0) != 'C' && itemNumber.charAt(0) != 'c' && itemNumber.charAt(0) != 'D' && itemNumber.charAt(0) != 'd' && itemNumber.charAt(0) != 'E'
&& itemNumber.charAt(0) != 'e' && itemNumber.charAt(0) != 'F' && itemNumber.charAt(0) != 'f' && itemNumber.charAt(0) != 'G' && itemNumber.charAt(0) != 'g'
&& itemNumber.charAt(0) != 'H' && itemNumber.charAt(0) != 'h' && itemNumber.charAt(0) != 'I' && itemNumber.charAt(0) != 'i' && itemNumber.charAt(0) != 'J'
&& itemNumber.charAt(0) != 'j' && itemNumber.charAt(0) != 'K' && itemNumber.charAt(0) != 'k' && itemNumber.charAt(0) != 'L' && itemNumber.charAt(0) != 'l'
&& itemNumber.charAt(0) != 'M' && itemNumber.charAt(0) != 'm' && itemNumber.charAt(0) != 'N' && itemNumber.charAt(0) != 'n' && itemNumber.charAt(0) != 'O'
&& itemNumber.charAt(0) != 'o' && itemNumber.charAt(0) != 'P' && itemNumber.charAt(0) != 'p' && itemNumber.charAt(0) != 'Q' && itemNumber.charAt(0) != 'q'
&& itemNumber.charAt(0) != 'R' && itemNumber.charAt(0) != 'r' && itemNumber.charAt(0) != 'S' && itemNumber.charAt(0) != 's' && itemNumber.charAt(0) != 'T'
&& itemNumber.charAt(0) != 't' && itemNumber.charAt(0) != 'U' && itemNumber.charAt(0) != 'u' && itemNumber.charAt(0) != 'V' && itemNumber.charAt(0) != 'v'
&& itemNumber.charAt(0) != 'W' && itemNumber.charAt(0) != 'w' && itemNumber.charAt(0) != 'X' && itemNumber.charAt(0) != 'x' && itemNumber.charAt(0) != 'Y'
&& itemNumber.charAt(0) != 'y' && itemNumber.charAt(0) != 'Z' && itemNumber.charAt(0) != 'z' && itemNumber.charAt(1) != 'A' && itemNumber.charAt(1) != 'a'
&& itemNumber.charAt(1) != 'B' && itemNumber.charAt(1) != 'b' && itemNumber.charAt(1) != 'C' && itemNumber.charAt(1) != 'c' && itemNumber.charAt(1) != 'D'
&& itemNumber.charAt(1) != 'd' && itemNumber.charAt(1) != 'E' && itemNumber.charAt(1) != 'e' && itemNumber.charAt(1) != 'F' && itemNumber.charAt(1) != 'f'
&& itemNumber.charAt(1) != 'G' && itemNumber.charAt(1) != 'g' && itemNumber.charAt(1) != 'H' && itemNumber.charAt(1) != 'h' && itemNumber.charAt(1) != 'I'
&& itemNumber.charAt(1) != 'i' && itemNumber.charAt(1) != 'J' && itemNumber.charAt(1) != 'j' && itemNumber.charAt(1) != 'K' && itemNumber.charAt(1) != 'k'
&& itemNumber.charAt(1) != 'L' && itemNumber.charAt(1) != 'l' && itemNumber.charAt(1) != 'M' && itemNumber.charAt(1) != 'm' && itemNumber.charAt(1) != 'N'
&& itemNumber.charAt(1) != 'n' && itemNumber.charAt(1) != 'O' && itemNumber.charAt(1) != 'o' && itemNumber.charAt(1) != 'P' && itemNumber.charAt(1) != 'p'
&& itemNumber.charAt(1) != 'Q' && itemNumber.charAt(1) != 'q' && itemNumber.charAt(1) != 'R' && itemNumber.charAt(1) != 'r' && itemNumber.charAt(1) != 'S'
&& itemNumber.charAt(1) != 's' && itemNumber.charAt(1) != 'T' && itemNumber.charAt(1) != 't' && itemNumber.charAt(1) != 'U' && itemNumber.charAt(1) != 'u'
&& itemNumber.charAt(1) != 'V' && itemNumber.charAt(1) != 'v' && itemNumber.charAt(1) != 'W' && itemNumber.charAt(1) != 'w' && itemNumber.charAt(1) != 'X'
&& itemNumber.charAt(1) != 'x' && itemNumber.charAt(1) != 'Y' && itemNumber.charAt(1) != 'y' && itemNumber.charAt(1) != 'Z' && itemNumber.charAt(1) != 'z')
{
System.out.println("**Error 02** - Item Number Must Start With Two Letters");
}
} while(itemNumber.length() > 5 && itemNumber.charAt(0) != 'A' || itemNumber.charAt(0) != 'a' || itemNumber.charAt(0) != 'B' || itemNumber.charAt(0) != 'b' || itemNumber.charAt(0) != 'C'
|| itemNumber.charAt(0) != 'c' || itemNumber.charAt(0) != 'D' || itemNumber.charAt(0) != 'd' || itemNumber.charAt(0) != 'E' || itemNumber.charAt(0) != 'e'
|| itemNumber.charAt(0) != 'F' || itemNumber.charAt(0) != 'f' || itemNumber.charAt(0) != 'G' || itemNumber.charAt(0) != 'g' || itemNumber.charAt(0) != 'H'
|| itemNumber.charAt(0) != 'h' || itemNumber.charAt(0) != 'I' || itemNumber.charAt(0) != 'i' || itemNumber.charAt(0) != 'J' || itemNumber.charAt(0) != 'j'
|| itemNumber.charAt(0) != 'K' || itemNumber.charAt(0) != 'k' || itemNumber.charAt(0) != 'L' || itemNumber.charAt(0) != 'l' || itemNumber.charAt(0) != 'M'
|| itemNumber.charAt(0) != 'm' || itemNumber.charAt(0) != 'N' || itemNumber.charAt(0) != 'n' || itemNumber.charAt(0) != 'O' || itemNumber.charAt(0) != 'o'
|| itemNumber.charAt(0) != 'P' || itemNumber.charAt(0) != 'p' || itemNumber.charAt(0) != 'Q' || itemNumber.charAt(0) != 'q' || itemNumber.charAt(0) != 'R'
|| itemNumber.charAt(0) != 'r' || itemNumber.charAt(0) != 'S' || itemNumber.charAt(0) != 's' || itemNumber.charAt(0) != 'T' || itemNumber.charAt(0) != 't'
|| itemNumber.charAt(0) != 'U' || itemNumber.charAt(0) != 'u' || itemNumber.charAt(0) != 'V' || itemNumber.charAt(0) != 'v' || itemNumber.charAt(0) != 'W'
|| itemNumber.charAt(0) != 'w' || itemNumber.charAt(0) != 'X' || itemNumber.charAt(0) != 'x' || itemNumber.charAt(0) != 'Y' || itemNumber.charAt(0) != 'y'
|| itemNumber.charAt(0) != 'Z' || itemNumber.charAt(0) != 'z' && itemNumber.charAt(1) != 'A' || itemNumber.charAt(1) != 'a' || itemNumber.charAt(1) != 'B'
|| itemNumber.charAt(1) != 'b' || itemNumber.charAt(1) != 'C' || itemNumber.charAt(1) != 'c' || itemNumber.charAt(1) != 'D' || itemNumber.charAt(1) != 'd'
|| itemNumber.charAt(1) != 'E' || itemNumber.charAt(1) != 'e' || itemNumber.charAt(1) != 'F' || itemNumber.charAt(1) != 'f' || itemNumber.charAt(1) != 'G'
|| itemNumber.charAt(1) != 'g' || itemNumber.charAt(1) != 'H' || itemNumber.charAt(1) != 'h' || itemNumber.charAt(1) != 'I' || itemNumber.charAt(1) != 'i'
|| itemNumber.charAt(1) != 'J' || itemNumber.charAt(1) != 'j' || itemNumber.charAt(1) != 'K' || itemNumber.charAt(1) != 'k' || itemNumber.charAt(1) != 'L'
|| itemNumber.charAt(1) != 'l' || itemNumber.charAt(1) != 'M' || itemNumber.charAt(1) != 'm' || itemNumber.charAt(1) != 'N' || itemNumber.charAt(1) != 'n'
|| itemNumber.charAt(1) != 'O' || itemNumber.charAt(1) != 'o' || itemNumber.charAt(1) != 'P' || itemNumber.charAt(1) != 'p' || itemNumber.charAt(1) != 'Q'
|| itemNumber.charAt(1) != 'q' || itemNumber.charAt(1) != 'R' || itemNumber.charAt(1) != 'r' || itemNumber.charAt(1) != 'S' || itemNumber.charAt(1) != 's'
|| itemNumber.charAt(1) != 'T' || itemNumber.charAt(1) != 't' || itemNumber.charAt(1) != 'U' || itemNumber.charAt(1) != 'u' || itemNumber.charAt(1) != 'V'
|| itemNumber.charAt(1) != 'v' || itemNumber.charAt(1) != 'W' || itemNumber.charAt(1) != 'w' || itemNumber.charAt(1) != 'X' || itemNumber.charAt(1) != 'x'
|| itemNumber.charAt(1) != 'Y' || itemNumber.charAt(1) != 'y' || itemNumber.charAt(1) != 'Z' || itemNumber.charAt(1) != 'z'
);
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks horrible. you should use the fact that chars are actually numeric, and can compare them as such:
If you're willing to support more exotic characters like é you can shorten that even more:
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juan Bonilla wrote:Hi as you can see Im doing the loop to first be able to test the user input to be no loger or shorter than 5 letters and than making sure charAt (0) and charAt(1) are tested to be letters but they dont seem to be able to test charAT(1) im not sure why and the DO loop doesnt work any suggestions?

Yes. I'm with Jim here: use a regex. Maybe something like:which will allow for any kind of letter. If you just want ASCII letters, use 'p{Alpha}'.

My suggestion would be to read the regex docs in the Pattern class.

Winston
 
Juan Bonilla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I Thank everyone for your kind help in this matter I'm learning Java with all this delete and redo situations
 
Marshal
Posts: 56600
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . My suggestion would be to read the regex docs in the Pattern class. . .
. . . and I would add this tutorial to your reading list
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!