Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Find the first character matching the input character in the given
     * string where the character has no letter preceding it.
     * 
     * @param text the string to test for the presence of the input character
     * @param inputChar the test character
     * @param fromIndex the index position of the string to start from
     * @return the position of the first character matching the input character
     *          in the given string where the character has no letter preceding it.
     */
    public static int firstCharAt(String text, int inputChar, int fromIndex) {
        int result = 0;

        while (result >= 0) {
            result = text.indexOf(inputChar, fromIndex);

            if (result == 0) {
                return result;
            } else if (result > 0) {
                // Check there is a whitespace or symbol before the hit character
                if (Character.isLetter(text.codePointAt(result - 1))) {
                    // The pre-increment is used in if and else branches.
                    if (++fromIndex >= text.length()) {
                        return -1;
                    } else {
                        // Test again from next candidate character
                        // This isn't the first letter of this word
                        result = text.indexOf(inputChar, fromIndex);
                    }
                } else {
                    return result;
                }
            }

        }

        return result;
    }

    /**
     * Returns the index of the given object in the given array of -1 if the
     * object is not contained in the array.
     */
    public static int indexOf(Object[] array, Object obj) {
        if (obj != null && array != null) {
            for (int i = 0; i < array.length; i++) {
                if (array[i] == obj) {
                    return i;
                }
            }
        }

        return -1;
    }
}