Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {

    public static boolean isTelNo(String value, int len) {
        try {
            if (value == null)
                return false;

            value = value.trim();

            if (isNum(1, value) == false || getLength(value) != len) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean isNum(int flg, String value) {
        try {
            if (value == null || getLength(value.trim()) == 0)
                return false;

            value = value.replaceAll("[,]", "");

            if (flg == 2)
                value = value.replaceAll("[-]", "");

            byte[] b = value.trim().getBytes();

            for (int i = 0; i < b.length; i++) {
                if (ascallnum(b[i]) == false)
                    return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static int getLength(String value) {
        try {
            int ret = 0;
            if (value == null)
                return ret;
            char[] chars = value.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                String target = String.valueOf(chars[i]);
                byte[] b = target.getBytes();
                if (b.length == 2) {
                    ret += 2;
                } else {
                    ret++;
                }
            }
            return ret;
        } catch (Exception e) {
            return -1;
        }
    }

    private static boolean ascallnum(byte byt) {
        try {
            if ((byt >= 48 && byt <= 57) == false)
                return false;
            else
                return true;

        } catch (Exception e) {
            return false;
        }
    }
}