Java - Write code to Check whether the first non-whitespace character of the given line of text is the asterix (*).

Requirements

Write code to Check whether the first non-whitespace character of the given line of text is the asterix (*).

Demo

/*
 * Copyright (c) 2001-2002, Marco Hunsicker. All rights reserved.
 *
 * This software is distributable under the BSD license. See the terms of the
 * BSD license in the documentation provided with this software.
 *///www.j ava  2s . c  o m
 
//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String line = "book2s.com";
        System.out.println(isLeadingAsterix(line));
    }

    /**
     * Checks whether the first non-whitespace character of the given line of text is the
     * asterix (&#042).
     *
     * @param line line of text to check.
     *
     * @return <code>true</code> if the first non-whitespace character of the given text
     *         is an asterix.
     */
    private static boolean isLeadingAsterix(String line) {
        if (line == null) {
            return false;
        }

        for (int i = 0, size = line.length(); i < size; i++) {
            int character = line.charAt(i);

            switch (character) {
            case ' ':
            case '\t':

                continue;

                // skip leading whitespace
            default:

                if (character == '*') {
                    return true;
                }
                return false;
            }
        }

        return false;
    }
}