繁体   English   中英

在字符串中写入信息

[英]Writing information in a String

我需要将行保存到字符串中

前任:

public function countOccurrences() {

    String Message = "Hello, how are you?"
    String trim = Mensaje.replaceAll(" ", "");


    char[] third = trim.toCharArray();

    for (int counter = 0; counter < third.length; counter++) {

        char ch = third[counter];
        int count = 0;

        for (int i = 0; i < third.length; i++) {

            if (ch == third[i]) {
                count++;
            }
        }


        boolean flag = false;
        for (int j = counter - 1; j >= 0; j--) {
            if (ch == third[j]) {
                flag = true;
            }
        }

        if (!flag) {

            trim = "Letra:" + ch + " se repite " + count + " veces ";
             // trim is a String where I need to store this information
        }


}

预期结果:

  1. 我需要计算字符串中每个字符的出现次数:消息
  2. 将此信息存储在 txt 文件中

前任:

newfile.txt里面有这些信息:

H, 2 times
e, 2 times
l, 2 times
o, 3 times
w, 1 times
a, 1 times
r, 1 times
e, 1 times
y, 1 times
u, 1 times

//

我试过有人发布它:

修剪 = 修剪 + "\\n" + ch + ", " + count + " veces \\n";

结果是文件中的这个:(更接近)

你好吗? H, 2次e, 2次l, 2次o, 3次,, 1次w, 1次a, 1次r, 1次y, 1次u, 1次?, 1次

问题从这里开始:

    for (int i = 0; i < third.length; i++) {

        if (ch == third[i]) {
            count++;
        }
    }

我可以想到两种解决方案。 正确的方法是使用从字符到计数的映射 如果这样做,您可以用以下内容替换整个 for 循环(不仅仅是上面摘录的部分):

Map letters = new HashMap<Character, Integer>();
for(char c : third) {
  Integer raw_count = letters.get(c);
  int count = raw_count == null ? 0 : raw_count.intValue();
  letters.put(c, count + 1);
}

然后遍历地图并打印出它的值。 如果该部分令人困惑,请发表评论。

如果您以前从未听说过地图,现在是了解它们的好时机。 如果您不能使用地图(作业...),那么我当然不会提供完整的代码示例。 但是您可以使用数组来解决它。 创建一个 26 元素的 int 数组。 然后你可以做一些类似letters[c - 'a']++事情。 花点时间消化一下——我们说数组中的第一个元素是a的数量,第二个是b的数量,等等。最初这些都是 0。然后我们取我们的字符查看 ( c ),从中减去,使其介于025 (不是 97 和 122)之间,并在那里增加总数。

番石榴方式:

Multiset<String> count = HashMultiset.create(Lists.charactersOf(message.replaceAll(" ", ""));
Files.write(Joiner.on("\n").join(Iterables.transform(count.entrySet(),
    new Function<Entry<String>, String>() {
        @Override public String apply(Entry<String> entry) {
            return String.format("%s, %d times", entry.getElement(), entry.getCount());
        }
    }, new File("whatever.txt"), Charsets.UTF_8);

或者其他的东西。

public function countOccurrences() {

String Message = "Hello, how are you?"
String trim = Mensaje.replaceAll(" ", "");


char[] third = trim.toCharArray();

for (int counter = 0; counter < third.length; counter++) {

    if (ch == ',') {
        continue;
    }

    char ch = third[counter];
    int count = 0;

    for (int i = 0; i < third.length; i++) {

        if (ch == third[i]) {
            count++;
        }
    }


    boolean flag = false;
    for (int j = counter - 1; j >= 0; j--) {
        if (ch == third[j]) {
            flag = true;
        }
    }

    if (!flag) {

        trim = "Letra:" + ch + " se repite " + count + " veces ";
         // trim is a String where I need to store this information
    }

}

暂无
暂无

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

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