Question: Hi there! Below code I am trying to accept only numeric (0-9) and - as input to set my account number; if user enters invalid
Hi there!
Below code I am trying to accept only numeric (0-9) and "-" as input to set my account number; if user enters invalid input, program should throw an exception if it is valid input program should go ahead with the second layer of conditioning (isCheckDigitCorrect() is another method with an algorithm, at this point that method is irrelevant) and if the second condition is also true my program should go ahead and set the account number else throw an exception.
my problem is that, my if statement always returns false, I tried debugging to see if my regex pattern was correct, I found out that if I declare a string, it matches the pattern returning true however if I enter the same input as the declared regex pattern i.e input accountNumber="123-123456" my test variable returns false. What could be the problem here? How can I fix this? Thanks in advance.

private void setAccountNumber(String accountNumber) { accountNumber = keyboard . nextLine (); test = accountNumber.matches String regex = "123-123456"; debug returns False; boolean test = regex. matches ("~[0-9-]+$"); test = regex.matches if (accountNumber . matches ("~ [0-9-]+$")) { debug returns True; if (isCheckDigitCorrect(accountNumber) ) { this. accountNumber = accountNumber; } else { throw new BadAccountInputException(" Bad account number; check digit failed."); } } else { throw new BadAccount InputException("Account number must consists of numbers separated by -, other non-numeric characters not allowed"
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
