Java - Write code to Return the index of the first whitespace character or '-' in line that is at or before start.

Requirements

Write code to Return the index of the first whitespace character or '-' in line that is at or before start.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String line = "bo o k 2s.com";
        int start = 2;
        System.out.println(findBreakBefore(line, start));
    }/* w  w w  .  j  a va2 s.com*/

    /**
     * Returns the index of the first whitespace character or '-' in <var>line</var>
     * that is at or before <var>start</var>. Returns -1 if no such character is found. 
     * @param line A string of text
     * @param start Where to begin testing
     */
    public static int findBreakBefore(String line, int start) {
        for (int i = start; i >= 0; --i) {
            char c = line.charAt(i);
            if (Character.isWhitespace(c) || c == '-')
                return i;
        }
        return -1;
    }
}