I'm pretty new with Java and it is my first time posting here and I'm trying to write a code that would let me check if the item (vehicle registration plate) inputted by the user is valid. The item has a format of: YYY-LL-SSSSSS, where
- Starts with a three-digit year (i.e. YYY-LL-SSSSSS).
- Continues with a dash i.e. ‘-‘
- Followed by a two-letter county/city identifier (i.e. YYY-LL-SSSSSS). The possible county/city identifier (i.e. LL) are as follows: CK, CE, CN, CW, DN, DL, GY, KE, KK, KY, LK, LD, LH, LM, LS, MH, MN, MO, OY, RN, SO, TY, WD, WH, WX, WW
- Continues with a dash i.e. ‘-‘
- Ends with a one- to six-digit sequence number (i.e. YYY-LL-SSSSSS)
My code should be able to determine whether the plate number is valid by checking all the characters in the string, so if for example the user inputs '222-DN-1055' the plate 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:
My instantiable class
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;
}
}
My application class
import javax.swing.JOptionPane;
public class ItemCheckerApp{
public static void main(String[] args){
//vars
String userInput;
String validity;
String response;
do{//objects
ItemChecker item;
item=new ItemChecker();
//input
userInput=JOptionPane.showInputDialog(null,"Enter your vehicle registration plate (YYY-LL-SSSSSS): ").toUpperCase();
//set
item.setUserInput(userInput);
//compute
item.computeValidity();
//get
validity=item.getValidity();
//output
JOptionPane.showMessageDialog(null,"Your vehicle registration plate is: " validity);
response=JOptionPane.showInputDialog(null,"Do you wish to check another plate? [Yes/No]?");
}while(response.equalsIgnoreCase("yes"));
}
}
The code doesn't really check if the plate is valid 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. Hope it makes sense what I'm trying to ask.
CodePudding user response:
here is a cleaned up version of your code, though its not complete. You can follow the style of the code to complete the rest of the logic.
public void computeValidity(){
if (userInput.length() < 8 && userInput.length()> 13){
//if length is not in between 8 to 13
return; //exits function
}
for(int i=0;i<userInput.length();i ) {
char c = userInput.charAt(i);
if (i <= 2) {
if (Character.isDigit(c)) {
//do whatever for true
} else {
//return false etc
}
} else if (i == 3 || i == 6) {
if (c == '-') {
//do whatever for true
} else {
//return false etc
}
} else if (i <= 5) {
//check for county identifiers
} else {
//finally check for digits
if (Character.isDigit(c)) {
} else {
}
}
}
}
CodePudding user response:
I'd suggest that you can try to split the userInput
. Solve the problem piece by piece.
I use Scanner
for this example
Scanner scan = new Scanner(System.in);
String userInputPlate;
while (true) {
userInputPlate = scan.nextLine();
if (userInputPlate.equalsIgnoreCase("end")) {
break;
}
// add the condition for 'SSSSSS'
// i.e. > 8
// I omit it for readability
if (userInputPlate.length() <= 13) {
System.out.println("Please try again");
}
}
String[] plateParts = userInputPlate.split("-");
String YYY = plateParts[0];
String LL = plateParts[1];
String SSSSSS = plateParts[2];
// notice I use "!" in if statement
for (int i = 0; i < YYY.length(); i ){
char[] YYYchar = YYY.toCharArray();
if (!Character.isDigit((YYYchar[i]))){
return;
}
}
// for loop for LL, I skip it
// do the same SSSSSS
for (int i = 0; i < SSSSSS.length(); i ){
char[] SSSSSSchar = SSSSSS.toCharArray();
if(!Character.isDigit(SSSSSSchar[i])){
return;
}
}
}
By doing so, you not only can debug with each segment easier, and it also improves the readability.
Hope this works for you.