java - Regex not valid when trying to invalidate alpha's and non-zeros -
java - Regex not valid when trying to invalidate alpha's and non-zeros -
wrote method takes in string , checks see follow conditions:
if string "quit", terminate program. if string value other integer, should homecoming "invalid input ". any negative integers , 0 should homecoming "invalid input".however, when passed in 10, returned "invalid input"?
please advise:
public static string validate(string input) { pattern pattern = pattern.compile(".*[^1-9].*"); stringbuilder results = new stringbuilder(); if (input.equals("quit")) { system.exit(1); } else if (!pattern.matcher(input).matches() == false) { results.append("invalid input "); results.append("'"); results.append(input); results.append("'"); } homecoming results.tostring(); }
what's wrong doing?
you should write pattern of expect instead of you're not. describe want simpler describe rest of it.
so expect :
pattern acceptpattern = pattern.compile("[1-9][0-9]*");
you may consider create conditional look simpler , right not using both !
, == false
@ same time:
which create :
if (!acceptpattern .matcher(input).matches()) {//invalid input code}
or
if (acceptpattern .matcher(input).matches() == false) {//invalid input code}
note :
you write if(!a == false)
=> if(a == true)
=> if(a)
inverse
java regex
Comments
Post a Comment