Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.text.TextUtils;

public class Main {
    public static final int parseIPAddress(String address) {
        if (TextUtils.isEmpty(address))
            throw new IllegalArgumentException("address cannot be null or empty");
        String[] tokens = address.split("\\.");
        boolean isOk = true;
        int ip = 0;
        if (tokens.length == 4) {
            for (int i = 0; i < 4; i++) {
                try {
                    short num = Short.parseShort(tokens[i]);
                    if (num < 0 || num > 255) {
                        isOk = false;
                        break;
                    }
                    ip |= (num << (8 * i));
                } catch (NumberFormatException ex) {
                    isOk = false;
                    break;
                }
            }
        } else
            isOk = false;

        if (!isOk)
            throw new IllegalArgumentException("address is not in the form X.X.X.X, with X in the range 0-255");

        return ip;
    }
}