Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008 Red Hat, Inc.
 * 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:
 *    Elliott Baron <ebaron@redhat.com> - initial API and implementation
 *******************************************************************************/

import java.io.IOException;

public class Main {
    private static final String DOT = ".";

    /**
     * Retrieves PID from filename with format [PREFIX][PID].[EXTENSION]
     * @param filename - the file name to parse
     * @param prefix - the prefix of the filename up to the PID
     * @return - the PID portion of the filename as an Integer
     * @throws IOException If PID can not be parsed.
     */
    public static Integer parsePID(String filename, String prefix) throws IOException {
        String pidstr = filename.substring(prefix.length(), filename.lastIndexOf(DOT));
        if (isNumber(pidstr)) {
            return Integer.valueOf(pidstr);
        } else {
            throw new IOException("Cannot parse PID from output file"); //$NON-NLS-1$
        }
    }

    /**
     * Determines if argument is a number
     * @param string - argument to test
     * @return - true if argument is a number
     */
    public static boolean isNumber(String string) {
        boolean result = true;
        char[] chars = string.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (!Character.isDigit(chars[i])) {
                result = false;
            }
        }
        return result;
    }
}