validating a string in Java returns incorrectly. Testing for length of string from scanner? -


string validation issue:

this method works part, theres apparent logic problem. if user hits enter @ console no input, should return "error! entry required" message, doesnt. have imagined would, since testing input of 1 or less chars

public string getchoicestring(string prompt, string s1, string s2) {     this.println(prompt);     string userchoice = this.sc.next();     string i;     boolean isvalid = false;     while (isvalid == false)     {         if (userchoice.equalsignorecase(s1) || userchoice.equalsignorecase(s2))         {             isvalid = true;         }         else if (userchoice.length() <= 1 || userchoice.equalsignorecase("")) {             system.out.println("error! entry required. try again.");             userchoice = this.sc.next();         }         else {             this.println("error! entry must " + s1 + " or " + s2 + ". try again.");             userchoice = this.sc.next();         }      }     return userchoice; 

from here create instance of class contains method. called console. call methods this:

public class consoletestapp {         public static void main(string[] args) {         system.out.println("welcome console tester application");         system.out.println();          //get console object         console console = iofactory.getconsoleio();         console.println("int test");         console.getintwithinrange("enter integer between -100 , 100: ", -100, 100);         console.println();         console.println("double test");         console.getdoublewithinrange("enter number between -100 , 100: ", -100, 100);         console.println();         console.println("required string test");         console.getrequiredstring("enter email address: ");         console.println();         console.println("string choice test");         console.getchoicestring("select 1 (x/y): ", "x", "y");               }     } 

it doesn't seem of happens when enter carriage return scanner#next. javadoc mandates matches on complete token delimiter.

the default delimiter scanner \p{javawhitespace}+. in essence, describes whole token having @ least 1 whitespace character in it.

now, let's inspect empty string. doesn't contain any character in it. so, if going match against default delimiter regex, fail:

scanner sc = new scanner(system.in); pattern ptn = sc.delimiter(); system.out.println(ptn); string empty = ""; string regex = "\\p{javawhitespace}+"; system.out.println(empty.matches(regex)); // prints false 

so, pattern doesn't match, , scanner block until matches something, a phrase.

so, instead of trying deal headache may induced next(), may looking use instead nextline(). in cases, want use nextline() when want match entire line of entry, , next() when you're processing multiple elements in single line.

string userchoice = this.sc.nextline(); // wherever scanner instance lives... 

this match on containing line separator, , since hitting return/enter produce that, match entire line enter, even if it's blank line.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -