I'm trying to write a code that would let me check if the item inputted by the user is valid. The item has a format of: NNN-LL-NNNNNN, where N is a number and L is a letter.
My code should be able to determine whether the item is valid by checking all the characters in the string, so if for example the user inputs '222-DN-1055' it is valid but if it's '2-DN-1055' then it is not.
I can only use java methods I learnt in my course that's why I'm trying to do it with a String Buffer. I can't use regex.
Now, this is the code I have done so far:
public class ItemChecker{
//vars
private String userInput;
private StringBuffer strBuff;
private String validity;
//constructor
public ItemChecker(){
strBuff=new StringBuffer();
}
//set
public void setUserInput(String userInput){
this.userInput=userInput;
}
//compute
public void computeValidity(){
for(int i=0;i<userInput.length();i++){
if (Character.isDigit(userInput.charAt(0))){
strBuff.append(userInput.charAt(0));
}
else if (Character.isDigit(userInput.charAt(1))){
strBuff.append(userInput.charAt(1));
}
else if (Character.isDigit(userInput.charAt(2))){
strBuff.append(userInput.charAt(2));
}
else if (userInput.charAt(3)=='-'){
strBuff.append(userInput.charAt(3));
}
else if (Character.isLetter(userInput.charAt(4))){
strBuff.append(userInput.charAt(4));
}
else if (userInput.charAt(4) == 'c' || userInput.charAt(i) == 'd'|| userInput.charAt(i) == 'g' || userInput.charAt(i) == 'k' || userInput.charAt(i) == 'l' || userInput.charAt(i) == 'm'|| userInput.charAt(i) == 'o'|| userInput.charAt(i) == 'r' || userInput.charAt(i) == 's' || userInput.charAt(i) == 't' || userInput.charAt(i) == 'w'){
strBuff.append(userInput.charAt(4));
}
else if (Character.isLetter(userInput.charAt(5))){
strBuff.append(userInput.charAt(5));
}
else if (userInput.charAt(5) == 'k' || userInput.charAt(i) == 'e' || userInput.charAt(i) == 'n' || userInput.charAt(i) == 'w' || userInput.charAt(i) == 'l' || userInput.charAt(i) == 'y' || userInput.charAt(i) == 'd'|| userInput.charAt(i) == 'h' || userInput.charAt(i) == 'm' || userInput.charAt(i) == 's' || userInput.charAt(i) == 'o' || userInput.charAt(i) == 'x'){
strBuff.append(userInput.charAt(5));
}
else if (userInput.charAt(6)=='-'){
strBuff.append(userInput.charAt(6));
}
else if (Character.isDigit(userInput.charAt(7))){
strBuff.append(userInput.charAt(7));
}
else if (Character.isDigit(userInput.charAt(8))){
strBuff.append(userInput.charAt(8));
}
else if (Character.isDigit(userInput.charAt(9))){
strBuff.append(userInput.charAt(9));
}
else if (Character.isDigit(userInput.charAt(10))){
strBuff.append(userInput.charAt(10));
}
else if (Character.isDigit(userInput.charAt(11))){
strBuff.append(userInput.charAt(11));
}
else if (Character.isDigit(userInput.charAt(12))){
strBuff.append(userInput.charAt(12));
}
else{
strBuff.append("Your registration plate is not valid.");
}
}
validity=strBuff.toString();
}
//get
public String getValidity(){
return validity;
}
}
The code does not really work and I have no clue how to proceed from here. Also how do I make sure that if a user inputs more than six numbers at the end, the code would be considered invalid as well.