简体   繁体   English

如何检查字符串中的字符是否与前一个字符相同?

[英]how to check if the char in a string is the same as the previous char?

The problem asks to compare if the string has consecutive same letters and rewrite it as the number of the letter plus the letter, for example, AAAAA as in 5A.问题要求比较字符串是否有连续的相同字母,并将其重写为字母加字母的数字,例如 5A 中的 AAAAA。 But when I use the if statement to make the comparison the output become some very long number instead of the desired result.但是当我使用 if 语句进行比较时,输出变成了一些非常长的数字而不是所需的结果。

Here is a portion of the problem: Run-length encoding is a simple compression scheme best used when a dataset consists primarily of numerous, long runs of repeated characters.这是问题的一部分:运行长度编码是一种简单的压缩方案,当数据集主要由大量重复字符的长运行组成时最适合使用。 For example, AAAAAAAAAA is a run of 10 A's.例如,AAAAAAAAAA 是 10 个 A 的运行。 We could encode this run using a notation like *A10, where the * is a special flag character that indicates a run, A is the symbol in the run, and 10 is the length of the run.我们可以使用像 *A10 这样的符号来编码这个运行,其中 * 是一个特殊的标志字符,表示一个运行,A 是运行中的符号,10 是运行的长度。

Here is the code:这是代码:

import java.util.Scanner;

public class RunLengthEncoding {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter input string: ");
    String s = input.nextLine();
    for (int a = 0; a < s.length(); a++) {
        if ((s.charAt(a) < 'A' || s.charAt(a) > 'Z')) {
            System.out.print("Bad input");
            System.exit(0);
        }
    }
    System.out.print("Enter flag character: ");
    char flag = input.nextLine().charAt(0);
    if (flag == '#' || flag == '$' || flag == '*' || flag == '&') {

        int count = 0;
        for (int i = 1; i < s.length(); i++) {
            if(s.charAt(i)=s.charAt(i-1));
            count++;

            if (count == 1)
                System.out.print(s.charAt(i));
            if (count == 2)
                System.out.print(s.charAt(i) + s.charAt(i));
            if (count == 3)
                System.out.print(s.charAt(i) + s.charAt(i) + s.charAt(i));
            else
                System.out.print(flag + s.charAt(i) + (count + 1));

        }

    } else
        System.out.print("Bad input");
}
}

Your problem is here:你的问题在这里:

if(s.charAt(i)=s.charAt(i-1));

First of all, you must compare char s using == not = .首先,您必须使用==而不是=来比较char Second, putting a ;其次,放一个; right after the if statement will cause it to terminate immediately.紧跟在if语句之后会导致它立即终止。 In other words, the following line is no longer part of the if statement.换句话说,以下行不再是if语句的一部分。 Change to:改成:

if(s.charAt(i) == s.charAt(i-1))

Edit编辑

Regarding your comment, something like this should work, though I didn't test it.关于你的评论,这样的事情应该有效,尽管我没有测试过。 Just replace your current large if block with the below:只需将您当前的大if块替换为以下内容:

if (flag == '#' || flag == '$' || flag == '*' || flag == '&') {
    char last = ' ';
    char curr = ' ';
    int count = 1;
    for (int i = 0; i < s.length(); i++) {
        last = curr;
        curr = s.charAt(i);
        if (curr == last) {
            count++;
        } else if (last != ' ') {
            System.out.print(("" + count) + last);
            count = 1;
        }
    }
    System.out.print(("" + count) + last);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM