Java AbstractMap Usage countLinesColumns(String text, int initialLinesCnt, int initialColumnsCnt)

Here you can find the source of countLinesColumns(String text, int initialLinesCnt, int initialColumnsCnt)

Description

Count lines and columns (in last line) in text.

License

Open Source License

Parameter

Parameter Description
text String.
initialLinesCnt initial value of lines counter.
initialColumnsCnt initial value of columns counter.

Return

entry(pair), first element is lines counter, second - columns counter.

Declaration

private static Map.Entry<Integer, Integer> countLinesColumns(String text, int initialLinesCnt,
        int initialColumnsCnt) 

Method Source Code

//package com.java2s;
// License as published by the Free Software Foundation; either

import java.util.AbstractMap;
import java.util.Map;

public class Main {
    /**//from ww  w  .  j a v  a 2  s . c  o m
     * Count lines and columns (in last line) in text.
     * @param text
     *        String.
     * @param initialLinesCnt
     *        initial value of lines counter.
     * @param initialColumnsCnt
     *        initial value of columns counter.
     * @return entry(pair), first element is lines counter, second - columns
     *         counter.
     */
    private static Map.Entry<Integer, Integer> countLinesColumns(String text, int initialLinesCnt,
            int initialColumnsCnt) {
        int lines = initialLinesCnt;
        int columns = initialColumnsCnt;
        boolean foundCr = false;
        for (char c : text.toCharArray()) {
            if (c == '\n') {
                foundCr = false;
                lines++;
                columns = 0;
            } else {
                if (foundCr) {
                    foundCr = false;
                    lines++;
                    columns = 0;
                }
                if (c == '\r') {
                    foundCr = true;
                }
                columns++;
            }
        }
        if (foundCr) {
            lines++;
            columns = 0;
        }
        return new AbstractMap.SimpleEntry<>(lines, columns);
    }
}

Related

  1. buildClause(String key, String... values)
  2. decodeIconUrl(String path)
  3. getClassType(Object obj)
  4. getSymmetricPropertyValueDifference( Properties propertyFileOne, Properties propertyFileTwo)
  5. parseEntityURI(final String uri)