使用队列来计算字符串

时间:2018-11-16 15:46:39

标签: java count queue

我对此程序有疑问。 使用方案中,用户应输入一个数字(要输入多少个字符串),然后输入该数量的字符串。

示例:

2
ASSSDDDAAA 
AAALLLOOOLLL

然后程序应计算字符串中重复字符的数量,并提供后缀计数。

输出(基于上面的String示例):

AS3D3A3
A3L3O3L3

它应该执行输入字符串的快捷方式。问题是我把两个字符串放在队列中,但是在程序运行时,我从未从第二个字符串得到输出,而且在第一个字符串A3中,我从未获得最后一个字符串,就像程序没有看到它(我写了ASSSDDDAAA和我得到AS3D3 idk为什么)。

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    Queue<String> kolejkaWyrazow = new LinkedList<String>();
    String wyraz;

    System.out.println("Podaj liczbe ciagow liczbowych");
    int liczbaCiagowWyrazowych = scanner.nextInt();
    for(int i = 0; i <= liczbaCiagowWyrazowych; i++) {
        wyraz = scanner.nextLine();
        kolejkaWyrazow.add(wyraz);
    }

    MarkerCutt(kolejkaWyrazow);

}

private static void MarkerCutt(Queue<String> kolejkaWyrazow) {

    String box;
    int countRepeats;

    for(int i = 0; i <= kolejkaWyrazow.size(); i++) {
        box = kolejkaWyrazow.remove();
        countRepeats = 1;
        for(int k = 0; k < box.length(); k++) {
            if (box.charAt(k) == box.charAt(k + 1)) {
                countRepeats++;
            } else {
                System.out.print(box.charAt(k));
                if (countRepeats <= 2) {
                    System.out.print(box.charAt(k));
                    countRepeats = 1;
                }
                if (countRepeats >= 3) {
                    System.out.print(countRepeats);
                    countRepeats = 1;
                }
            }
        }
    }
}

}

2 个答案:

答案 0 :(得分:0)

基于OP的原始代码,for循环存在一个问题,即对下一个字符的检查将超出String的末尾,从而导致运行时错误。

对于给定的String box中最后一个条目缺少输出,是因为在for本身的box.length()循环中生成了输出。因此,当末尾有一个重复的字符时,它永远不会显示。我怀疑可以用AAABBBC之类的东西显示相同的问题(缺少最后一个字符),该问题应显示A3B3C,但不会显示最后一个C

稍作修改的算法将是始终在字符更改时显示该字符,然后在需要时添加计数。

private static void MarkerCutt(Queue<String> kolejkaWyrazow)
{

    // the string we are processing from the queue
    String box;

    // number of repeats for a character in the string
    int countRepeats;

    while (!kolejkaWyrazow.isEmpty()) {
        box = kolejkaWyrazow.remove();
        countRepeats = 0;
        // we hold the current character and the previous character;
        //  at entry, set the prevChar to something that cannot be matched
        char curChar, prevChar = '\0';

        // loop over the whole string; as we are not looking ahead, we
        //  use the full length of the String
        for (int k = 0; k < box.length(); k++) {

            // get the character
            curChar = box.charAt(k);

            // if we are not the same as the previous, then we
            //   may need to output the count; but output the character
            //   we just found, regardless
            if (curChar != prevChar) {
                outputCount(countRepeats);
                countRepeats = 0;
                System.out.print(curChar);
            }
            else {
                ++countRepeats;
            }
            prevChar = curChar;
        }
        // have to output the end of the string
        outputCount(countRepeats);
        System.out.println();
    }
}

private static void outputCount(int countRepeats) 
{
    if (countRepeats > 0) {
        System.out.print(countRepeats + 1);
    }
}

基于显示的输入(加上AAABBBC和'AAABBBCC`:

AS3D3A3
A3L3O3L3
A3B3C
A3B3C2

在更广泛的主题上,我会考虑从方法MarkerCutt中删除实际的输出,而是使用一个返回String的方法。将输出与逻辑分离通常是一个好主意。所以:

private static void MarkerCutt(Queue<String> kolejkaWyrazow)
{


    while (!kolejkaWyrazow.isEmpty()) {
        String box = kolejkaWyrazow.remove();
        String repString = changeToRepeatCount(box);
        System.out.println(repString);
    }
}

private static String changeToRepeatCount(String box)
{
    // number of repeats for a character in the string
    int countRepeats = 0;

    // We build into this structure
    StringBuilder sb = new StringBuilder();

    // we hold the current character and the previous character;
    //  at entry, set the prevChar to something that cannot be matched
    char curChar, prevChar = '\0';

    // loop over the whole string; as we are not looking ahead, we
    //  use the full length of the String
    for (int k = 0; k < box.length(); k++) {

        // get the character
        curChar = box.charAt(k);

        // if we are not the same as the previous, then we may need
        if (curChar != prevChar) {
            outputCount(countRepeats, sb);
            countRepeats = 0;
            sb.append(curChar);
        }
        else {
            ++countRepeats;
        }
        prevChar = curChar;
    }
    // have to output the end of the string
    outputCount(countRepeats, sb);

    return sb.toString();
}

private static void outputCount(int countRepeats, StringBuilder sb) 
{
    if (countRepeats > 0) {
        sb.append(countRepeats + 1);
    }
}

后一种方法可以进行一些更好的/自动化的测试,因为然后可以编写执行以下操作的驱动程序:

String inp = "ASSSDDDAAA";
    String exp = "AS3D3A3";
    String res = changeToRepeatCount(inp);
    if (! exp.equals(res)) {
        System.err.println("Test Failed!");
    }

(或者最好还是使用JUnit之类的东西)。这将使开发变得比每次都要键入一个值来测试代码都容易得多。只是想一想。

答案 1 :(得分:0)

document.addEventListener('DOMContentLoaded', function () {
    var Modalelem = document.querySelector('.modal');
    var instanceModal = M.Modal.init(Modalelem);
    instanceModal.open();
});

输入数字并按Enter键时,会将空字符串添加到kolejkaWyrazow,因此我添加了“ if(wyraz.trim()。length()> 0)”以避免这种情况

然后我重写您的MarkerCutt方法,请参见下面的

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    Queue<String> kolejkaWyrazow = new LinkedList<String>();
    String wyraz;

    System.out.println("Podaj liczbe ciagow liczbowych");
    int iCount = scanner.nextInt();
    for (int i = 0; i <= iCount; i++) {
        wyraz = scanner.nextLine();

        if (wyraz.trim().length() > 0) {
            kolejkaWyrazow.add(wyraz);
        }
    }

    MarkerCutt(kolejkaWyrazow);
}

测试:

private static void MarkerCutt(Queue<String> queue) {
    for (int i = 0; i <= queue.size(); i++) {
        StringBuilder sb = new StringBuilder();
        String box = queue.remove();

        char c = box.charAt(0);
        int iCount = 1;

        for (int j = 1; j < box.length(); j++) {
            if (box.charAt(j) == c) {
                iCount++;
            } else {
                sb.append(c + "" + iCount);

                c = box.charAt(j);
                iCount = 1;
            }
        }

        sb.append(c + "" + iCount);

        System.out.println(sb.toString());
    }
}

结果

Podaj liczbe ciagow liczbowych
2
ASSSDDDAAA
AAALLLOOOLLL

如果您不想打印不重复的char的计数(count = 1):

更改

A1S3D3A3
A3L3O3L3

sb.append(c + "" + iCount);

测试

sb.append(c + "" + (iCount > 1 : iCount : ""));

结果:

Podaj liczbe ciagow liczbowych
2
ASSSDDDAAA
AAALLLOOOLLL