计算字符串中字符的出现次数 [英] Counting the number of occurences of characters in a string

查看:244
本文介绍了计算字符串中字符的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Java程序,它以输入的形式输入一个字符串,并计算一个字符串中出现的字符数,然后打印一个新的字符串,后面加上没有出现的字符。



EG



输入字符串:

  aaaabb 

输出字符串:

  a4b2 

输入字符串:

  aaaaabbbc 

输出字符串:

  a5b3c1 

我正在发布我的java代码。

它正在抛出 StringOutOfBoundException

  / *编写一个例程,作为输入一个字符串,如aabbccdef和o / pa2b2c2def或a4bd2g4用于aaaabddgggg* / 

import java.util.Scanner;

public class CountingOccurences {

public static void main(String [] args){

扫描仪inp = new Scanner(System.in);
String str;
char ch
int count = 0;

System.out.println(输入字符串:);
str = inp.nextLine();

while(str.length()> 0)
{
ch = str.charAt(0);
int i = 0;

while(str.charAt(i)== ch)
{
count = count + i;
i ++;
}

str.substring(count);
System.out.println(ch);
System.out.println(count);
}

}

}


解决方案

这是问题:

  while(str.charAt(i)= = ch)

这将持续进行,直到它脱离结束...当 i 与字符串的长度相同,它将要求超出字符串结尾的字符。你可能想要:

  while(i< str.length()&& str.charAt(i)== ch)

您还需要设置 count 在更大循环的每次迭代开始时为0 - 计数重置,并且更改

  count = count +一世; 

至:

 计数++; 

...或摆脱计数 i 。毕竟,他们总是有同样的价值。我个人只是使​​用一个变量,在循环中声明并初始化了。这是一个通用的风格点,实际上 - 在需要时声明局部变量更为清晰,而不是将其全部声明在方法的顶部。



然而,那么你的程序将永远循环,因为这没有任何有用的东西:

  str.substring(count); 

字符串在Java中是不可变的 - substring 返回一个新的字符串。我想你想要的:

  str = str.substring(count); 

请注意,这仍然会为aabbaa输出a2b2a2。可以吗?


I am trying to write a Java program which takes in as input a string and counts the number of occurrences of characters in a string and then prints a new string having the character followed by the no of occurrences.

E.G.

Input String:

aaaabb

Output String:

a4b2

Input String:

aaaaabbbc

Output String:

a5b3c1

I am posting my java code.
It is throwing StringOutOfBoundException

/*Write a routine that takes as input a string such as "aabbccdef" and o/p "a2b2c2def" or "a4bd2g4" for "aaaabddgggg".*/

import java.util.Scanner;

public class CountingOccurences {

public static void main(String[] args) {

    Scanner inp= new Scanner(System.in);
    String str;
    char ch;
    int count=0;

    System.out.println("Enter the string:");
    str=inp.nextLine();

    while(str.length()>0)
    {
        ch=str.charAt(0);
        int i=0;

        while(str.charAt(i)==ch)
        {
                count =count+i;
                i++;
        }

        str.substring(count);
        System.out.println(ch);
        System.out.println(count);
    }

}

}

解决方案

This is the problem:

while(str.charAt(i)==ch)

That will keep going until it falls off the end... when i is the same as the length of the string, it will be asking for a character beyond the end of the string. You probably want:

while (i < str.length() && str.charAt(i) == ch)

You also need to set count to 0 at the start of each iteration of the bigger loop - the count resets, after all - and change

count = count + i;

to either:

count++;

... or get rid of count or i. They're always going to have the same value, after all. Personally I'd just use one variable, declared and initialized inside the loop. That's a general style point, in fact - it's cleaner to declare local variables when they're needed, rather than declaring them all at the top of the method.

However, then your program will loop forever, as this doesn't do anything useful:

str.substring(count);

Strings are immutable in Java - substring returns a new string. I think you want:

str = str.substring(count);

Note that this will still output "a2b2a2" for "aabbaa". Is that okay?

这篇关于计算字符串中字符的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆