Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions