Can't get java to match regex with matches() -



Can't get java to match regex with matches() -

i have problem i'm trying parameter match format 'uddd' 'u' must letter u , 'ddd' can 3 digits 0-9.

my current code:

//borrow method public boolean borrow(string borrowerid) { //if borrower id matches format 'uddd' if (borrowerid.matches("u([0-9]{3})")) { //if status available if (status == 'a') { this.borrowerid = borrowerid; this.status = 'o'; this.dateborrowed = currentdate; homecoming true; } //is not available else { homecoming false; } } //does not match format else { homecoming false; } }

for reason it's not validating properly. when tried inputting '1' parameter, still returned true.

is there i'm missing?

it should not possible method homecoming true if input "1". can suggest ensure are passing in "1" , method 1 beingness called.

that can done simple debug statement @ top, like:

system.out.println ("calling right function [" + borrowerid + "]");

at start of function.

i'd suggest bit of clean-up create function easier code , read, along lines of:

// borrow method public boolean borrow(string borrowerid) { // temp debug statement. // system.out.println ("borrow [" + borrowerid + "]"); // "not available" or "invalid borrower" means reject request. if (status != 'a') homecoming false; if (! borrowerid.matches("u([0-9]{3})")) homecoming false; // okay borrow. this.borrowerid = borrowerid; this.status = 'o'; this.dateborrowed = currentdate; homecoming true; }

this lot cleaner return-else-do-something constructs, , follows "fail fast" paradigm.

some people tend dislike multiple homecoming points that's because don't understand why they're considered bad (spaghetti code). short function this, doesn't pose problem.

java regex

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -