Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * %W% %E%
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

public class Main {
    /** Parse a version string such as "1.1.6" or "jdk1.2fcs" into
    a version array of integers {1, 1, 6} or {1, 2}.
    A string of "n." or "n..m" is equivalent to "n.0" or "n.0.m" respectively.
     */
    public static int[] parseVersion(String version) {
        if (version == null)
            return new int[0];
        char[] s = version.toCharArray();
        //find the maximum span of the string "n.n.n..." where n is an integer
        int start = 0;
        for (; start < s.length && (s[start] < '0' || s[start] > '9'); ++start)
            if (start == s.length) //no digit found
                return new int[0];
        int end = start + 1;
        int size = 1;
        for (; end < s.length; ++end)
            if (s[end] == '.')
                ++size;
            else if (s[end] < '0' || s[end] > '9')
                break;
        int[] val = new int[size];
        for (int i = 0; i < size; ++i) {
            int dot = version.indexOf('.', start);
            if (dot == -1 || dot > end)
                dot = end;
            if (start >= dot) //cases like "n." or "n..m"
                val[i] = 0; //convert equivalent to "n.0" or "n.0.m"
            else
                val[i] = Integer.parseInt(version.substring(start, dot));
            start = dot + 1;
        }
        return val;
    }
}