java - checking a String for anything other than an integer or character a - z -
this dealing java start. goal take in vin number , store string. make sure has no more 9 characters. vin number supposed contain numbers , letters a-z or a-z. if user inputs invalid vin number should loop around , prompt new vin number. problem telling whether string contains other integers or letters. i've seen utility.isalphanumeric
method can't figure out how implement or whether it's java method. netbeans gives me errors whenever try use it.
private static string checkvin() { scanner input = new scanner(system.in); string vinnumber = ""; { system.out.println("please enter vin number of car"); vinnumber = input.next(); if (vinnumber.length() > 9) { system.out.println("invalid input vin number"); } (int = 0; < vinnumber.length(); i++) { char currentcharacter = vinnumber.charat(i); if ((currentcharacter < '0' || currentcharacter > '9') || !( currentcharacter < 'a' || currentcharacter > 'z') || !(currentcharacter > 'a' || currentcharacter > 'z')); system.out.println("invalid input vin number"); break; } return vinnumber; } while (true); }
utility
not name of class core libraries.
you can replace entire check loop with
import java.util.regex.pattern; import java.util.regex.matcher; private static final pattern valid_vin_number = pattern.compile( "[0-9a-za-z]{1,9}"); public static boolean isvin(string s) { return valid_vin_number.matcher(s).matches(); }
Comments
Post a Comment