Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Try to split the given value to String[2]. The result format is as
     * follows: [number][other]. If either part can not be determined, it will
     * leave null.
     * 
     * @param value
     *            given string value
     * @return [number][other]
     */
    public static String[] splitString(String value) {
        String[] spt = new String[2];

        if (value != null) {
            for (int i = value.length(); i > 0; i--) {
                if (isValidNumber(value.substring(0, i))) {
                    spt[0] = value.substring(0, i);
                    spt[1] = value.substring(i, value.length());

                    break;
                }
            }

            if (spt[0] == null && spt[1] == null) {
                spt[1] = value;
            }
        }

        return spt;
    }

    /**
     * Checks if the value is a valid number, including any integer and float,
     * double.
     * 
     * @param val
     */
    public static boolean isValidNumber(String val) {
        try {
            Double.parseDouble(val);
        } catch (Exception e) {
            return false;
        }

        return true;
    }
}