• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validating Data On A Method  RSS feed

 
Juan Bonilla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day, as I'm a Java Student I have been asked on a homework, to create a method to validate input from the user, and return it as weather good input or bad input, the user should enter a code that is
2 letter and 3 numbers I.E AB123 i believe i can create a method to validate numbers input for instace i had created:

public static boolean editItemNum(int number) {
boolean statusNum;
if (number >=0 && number <= 999)
statusNum = true;
else
statusNum = false;
Return statusNum;

My question would be how can i validate users Input that corresponds to entering data using java util scanner and then calling a method that validates a value of AB123 for instance but it would not allow ABC12 or any incorrect input ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need to change your method parameter to be a String instead of an int.
you should give your method a more meaningful name - something like isValidCode.
In your method you will need to test each character of the String individually - the String class has a method that will give you individual characters.
The Character class then has methods that will test whether each character is a letter or digit.

Edit : You could also test the length of the String first - if it's not five characters long you can immediately return false without having to check any characters
 
Juan Bonilla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amaaazing ! thanks very much
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!