Example usage for java.lang StringIndexOutOfBoundsException StringIndexOutOfBoundsException

List of usage examples for java.lang StringIndexOutOfBoundsException StringIndexOutOfBoundsException

Introduction

In this page you can find the example usage for java.lang StringIndexOutOfBoundsException StringIndexOutOfBoundsException.

Prototype

public StringIndexOutOfBoundsException(int index) 

Source Link

Document

Constructs a new StringIndexOutOfBoundsException class with an argument indicating the illegal index.

Usage

From source file:Main.java

public static void main(String[] args) {
    Object o = new String("Hello");

    Class clazz = o.getClass().getSuperclass();
    System.out.println("Super Class = " + clazz);

    o = new StringIndexOutOfBoundsException("Error message");

    clazz = o.getClass().getSuperclass();
    System.out.println("Super Class = " + clazz);
}

From source file:Main.java

/**
 * <p>/*ww w.  j a va2s.  com*/
 * Gets <code>n</code> characters from the middle of a String.
 * </p>
 * <p/>
 * <p>
 * If <code>n</code> characters are not available, the remainder of the String will be returned without an
 * exception. If the String is <code>null</code>, <code>null</code> will be returned.
 * </p>
 *
 * @param str the String to get the characters from
 * @param pos the position to start from
 * @param len the length of the required String
 * @return the leftmost characters
 * @throws IndexOutOfBoundsException if pos is out of bounds
 * @throws IllegalArgumentException if len is less than zero
 */
public static String mid(String str, int pos, int len) {
    if ((pos < 0) || ((str != null) && (pos > str.length()))) {
        throw new StringIndexOutOfBoundsException("String index " + pos + " is out of bounds");
    }
    if (len < 0) {
        throw new IllegalArgumentException("Requested String length " + len + " is less than zero");
    }
    if (str == null) {
        return null;
    }
    if (str.length() <= (pos + len)) {
        return str.substring(pos);
    } else {
        return str.substring(pos, pos + len);
    }
}

From source file:CharArraySequence.java

public char charAt(int index) {
    if (index < 0 || index >= count) {
        throw new StringIndexOutOfBoundsException(index);
    }//from www  . ja va2  s .  c om
    return buff[offset + index];
}

From source file:CharArraySequence.java

public CharSequence subSequence(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }//  ww  w . java  2s.co  m
    if (endIndex > count) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    if (beginIndex > endIndex) {
        throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    }
    return ((beginIndex == 0) && (endIndex == count)) ? this
            : new CharArraySequence(buff, offset + beginIndex, endIndex - beginIndex);
}

From source file:CharSequenceDemo.java

public char charAt(int i) {
    if ((i < 0) || (i >= s.length())) {
        throw new StringIndexOutOfBoundsException(i);
    }/*from   w  ww  .ja  v  a 2s.  c o m*/
    return s.charAt(fromEnd(i));
}

From source file:CharSequenceDemo.java

public CharSequence subSequence(int start, int end) {
    if (start < 0) {
        throw new StringIndexOutOfBoundsException(start);
    }//from  www  .ja  va 2s.  c  o  m
    if (end > s.length()) {
        throw new StringIndexOutOfBoundsException(end);
    }
    if (start > end) {
        throw new StringIndexOutOfBoundsException(start - end);
    }
    StringBuilder sub = new StringBuilder(s.subSequence(fromEnd(end), fromEnd(start)));
    return sub.reverse();
}

From source file:org.mariotaku.twidere.util.HtmlBuilder.java

public boolean addLink(final String link, final String display, final int start, final int end,
        final boolean displayIsHtml) {
    if (start < 0 || end < 0 || start > end || end > sourceLength) {
        final String message = String.format(Locale.US, "text:%s, length:%d, start:%d, end:%d", source,
                sourceLength, start, end);
        if (throwExceptions)
            throw new StringIndexOutOfBoundsException(message);
        return false;
    }/* w  w  w .java2  s.  c om*/
    if (hasLink(start, end)) {
        final String message = String.format(Locale.US,
                "link already added in this range! text:%s, link:%s, display:%s, start:%d, end:%d", source,
                link, display, start, end);
        if (throwExceptions)
            throw new IllegalArgumentException(message);
        return false;
    }
    return spanSpecs.add(new LinkSpec(link, display, start, end, displayIsHtml));
}

From source file:org.kuali.kfs.module.ar.document.service.impl.CustomerServiceImpl.java

/**
 * @see org.kuali.kfs.module.ar.document.service.CustomerService#getNextCustomerNumber(org.kuali.kfs.module.ar.businessobject.Customer)
 *///w w w.  j  av a  2s.co  m
@Override
public String getNextCustomerNumber(Customer newCustomer) {
    try {
        Long customerNumberSuffix = sequenceAccessorService
                .getNextAvailableSequenceNumber(CUSTOMER_NUMBER_SEQUENCE, Customer.class);
        String customerNumberPrefix = newCustomer.getCustomerName().substring(0, 3);
        String customerNumber = customerNumberPrefix + String.valueOf(customerNumberSuffix);

        return customerNumber;
    } catch (StringIndexOutOfBoundsException sibe) {
        // The customer number generation expects all customer names to be at least three characters long.
        throw new StringIndexOutOfBoundsException("Customer name is less than three characters in length.");
    }
}

From source file:org.trnltk.model.letter.TurkishSequence.java

/**
 * Creates a subsequence starting at index {@code beginIndex} and ending at index {@code endIndex}.
 * <p/>/*from   w w w  .j ava2 s .co  m*/
 * Returned subsequence is not a view, but a full-copy.
 *
 * @param beginIndex The begin index
 * @param endIndex   The end index
 * @return A new {@link TurkishSequence} instance as subsequence
 */
public TurkishSequence subsequence(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > this.count) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    if (beginIndex > endIndex) {
        throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    }

    if (((beginIndex == 0) && (endIndex == this.count)))
        return this;

    return new TurkishSequence(Arrays.copyOfRange(this.chars, beginIndex, endIndex));
}

From source file:com.puppycrawl.tools.checkstyle.checks.coding.CustomDeclarationOrderCheck.java

/**
 * Parse input current declaration rule and create new instance of
 * FormatMather with matcher/*from   w ww  .jav a2s.  co m*/
 *
 * @param aCurrentState input string with MemberDefinition and RegExp.
 * @return new FormatMatcher with parsed and compile rule
 */
private FormatMatcher parseInputDeclarationRule(final String aCurrentState) {
    String classMember;
    String regExp;
    try {
        // parse mClassMember
        classMember = aCurrentState.substring(0, aCurrentState.indexOf('(')).trim();
        final String classMemberNormalized = normalizeMembersNames(classMember.toLowerCase());
        if (classMember.toLowerCase().equals(classMemberNormalized)) {
            // if Class Member has been specified wrong
            throw new ConversionException("unable to parse " + classMember);
        } else {
            classMember = classMemberNormalized;
        }

        // parse regExp
        regExp = aCurrentState.substring(aCurrentState.indexOf('(') + 1, aCurrentState.lastIndexOf(')'));
        if (regExp.isEmpty()) {
            regExp = "package"; // package level
        }

    } catch (StringIndexOutOfBoundsException exp) {
        //if the structure of the input rule isn't correct
        throw new StringIndexOutOfBoundsException("unable to parse input rule: " + aCurrentState + " " + exp);
    }

    final FormatMatcher matcher = new FormatMatcher(aCurrentState, classMember, mCompileFlags);
    matcher.updateRegexp(regExp, mCompileFlags);
    return matcher;
}