Java Array Index Of arrayIndexFor(final String raw)

Here you can find the source of arrayIndexFor(final String raw)

Description

DOCUMENT ME!

License

LGPL

Parameter

Parameter Description
raw DOCUMENT ME!

Return

DOCUMENT ME!

Declaration

protected static int arrayIndexFor(final String raw) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**//from   w w w. ja v  a 2  s .co  m
     * DOCUMENT ME!
     *
     * @param   raw  DOCUMENT ME!
     *
     * @return  DOCUMENT ME!
     */
    protected static int arrayIndexFor(final String raw) {
        /*
         * Empty? No dice.
         */
        if (raw.isEmpty()) {
            return -1;
        }
        /*
         * Leading zeroes are not allowed in number-only refTokens for arrays. But then, 0 followed by anything else
         * than a number is invalid as well. So, if the string starts with '0', return 0 if the token length is 1 or -1
         * otherwise.
         */
        if (raw.charAt(0) == '0') {
            return (raw.length() == 1) ? 0 : -1;
        }

        /*
         * Otherwise, parse as an int. If we can't, -1.
         */
        try {
            return Integer.parseInt(raw);
        } catch (NumberFormatException ignored) {
            return -1;
        }
    }
}

Related

  1. arrayIndex(T[] arr, T item)
  2. arrayIndexCheck(char[] text, int startPos, int endPos)
  3. arrayIndexClean(String indx)
  4. ArrayIndexOf(byte[] data, byte[] dest)
  5. arrayIndexOf(int needle, int[] haystack)
  6. arrayIndexOf(int value, int[] arr)
  7. arrayIndexOf(Object[] array, Object element)