Java Scanner Usage wrap(String msg, int wrapWidth, int wrapTolerance)

Here you can find the source of wrap(String msg, int wrapWidth, int wrapTolerance)

Description

Attempts to wrap the supplied string with the given width.

License

Open Source License

Parameter

Parameter Description
msg the message to be wrapped
wrapWidth the maximum width of a single row of text. Defaults to 50 if negative number or 0.
wrapTolerance how many characters over the limit words can go. Defaults to 5 if negative number.

Return

wrapped string

Declaration

public static String wrap(String msg, int wrapWidth, int wrapTolerance) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Scanner;

public class Main {
    /**//  www .jav  a2s .c  o m
     * Attempts to wrap the supplied string with the given width.
     * 
     * @param msg
     *            the message to be wrapped
     * @param wrapWidth
     *            the maximum width of a single row of text.
     *            Defaults to 50 if negative number or 0.
     * @param wrapTolerance
     *            how many characters over the limit words can go.
     *            Defaults to 5 if negative number.
     * @return wrapped string
     * 
     * @see #wrapOS(String, int, int, OS...)
     * @see #wrapOSNot(String, int, int, OS...)
     */
    public static String wrap(String msg, int wrapWidth, int wrapTolerance) {
        if (msg == null)
            throw new IllegalArgumentException("Argument must not be null.");
        if (wrapWidth <= 0)
            wrapWidth = 50;
        if (wrapTolerance < 0)
            wrapTolerance = 5;
        StringBuilder buf = new StringBuilder();
        Scanner sc = new Scanner(msg);

        int currentWidth = 0;
        while (sc.hasNext()) {
            String token = sc.next();
            int newWidth = currentWidth + token.length();
            if (currentWidth >= wrapWidth) {
                // Even without the new token it's already outside -- move to new line
                buf.append("\n");
                buf.append(token);
                currentWidth = token.length();
            } else if (newWidth > wrapWidth + wrapTolerance) {
                // Falls outside of the allowed wrap width -- split
                String[] split = split(token, wrapWidth - currentWidth - 1);
                if (!split[0].isEmpty()) {
                    buf.append(" ");
                    buf.append(split[0]);
                }
                buf.append(split[0].isEmpty() || split[1].isEmpty() ? "\n" : "-\n");
                buf.append(split[1]);
                currentWidth = split[1].length();
            } else {
                // Fits within the wrap width -- append to current line
                if (currentWidth > 0)
                    buf.append(" ");
                buf.append(token);
                currentWidth += token.length() + 1;
            }
        }

        return buf.toString();
    }

    private static String[] split(String word, int splitIndex) {
        String[] result = new String[2];
        if (splitIndex == 0) {
            result[0] = "";
            result[1] = word;
            return result;
        } else if (splitIndex == word.length()) {
            result[0] = word;
            result[1] = "";
            return result;
        }

        int i = findIndexOfClosestVowel(word, splitIndex);

        if (i < 0) {
            result[0] = word;
            result[1] = "";
        } else {
            result[0] = word.substring(0, i);
            result[1] = word.substring(i);
        }

        return result;
    }

    private static int findIndexOfClosestVowel(String word, int splitIndex) {
        char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'y' };
        int result = -word.length();
        for (char vowel : vowels) {
            int indexFrom = word.indexOf(vowel, splitIndex);
            int indexTo = word.substring(0, splitIndex - 1).indexOf(vowel);
            // Determine which one is closer
            int candidate = Math.abs(splitIndex - indexFrom) < Math.abs(splitIndex - indexTo) ? indexFrom : indexTo;
            result = Math.abs(splitIndex - result) < Math.abs(splitIndex - candidate) ? result : candidate;
        }
        return result;
    }

    public static int indexOf(Object[] array, Object object) {
        for (int i = 0; i < array.length; i++) {
            if (array[i].equals(object))
                return i;
        }
        return -1;
    }
}

Related

  1. transformTXTandANNtoRQS(String TXT, String ANN)
  2. trim(String s)
  3. validateInt(Scanner keyboard, String message, String error)
  4. waitForEnter()
  5. waitForStopSignal()