Java String to complete mathematical sum

后端 未结 3 676
礼貌的吻别
礼貌的吻别 2021-01-28 07:24

In java, I am currently in the process of trying to find a way to do mathematical sums by typing in text to a JTextField, putting it into a String, and converting it to an int a

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-28 08:07

    See to java.util.regex.Matcher or java.util.regex.Pattern For ex:

    import java.util.regex.Matcher; import java.util.regex.Pattern;
    
    public class IpDnsValidateUtils {
        public static final int MAX_DNS_LEN = 39;
        private static final Pattern ptrnIp = Pattern
            .compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"); 
        private static final Pattern ptrnDomainName = Pattern
            .compile("^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])(\\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])){0,}[.][a-zA-Z]{2,5}$"); 
    
        public static boolean isValidIp(String value, boolean emptyTrue) {  boolean ret = false;    if (!value.isEmpty() || !emptyTrue) {
            Matcher m = ptrnIp.matcher(value);
            ret = m.matches();  } else {
            ret = true;     }   return ret;
        }
    
        public static boolean isValidDns(String value, boolean emptyTrue) {     boolean ret = false;    if (!value.isEmpty() || !emptyTrue) {
            Matcher m = ptrnDomainName.matcher(value);
            ret = m.matches();  } else {
            ret = true;     }   return ret;
        } }
    

    or StringTokenizer class

提交回复
热议问题