繁体   English   中英

读取字符在字符串中出现多少次时出错

[英]errors reading how many times a character occurs in a string

我正在极度沮丧的时间内尝试从我的程序之一中删除该错误。 基本上,我的程序接收一个字符串,并读取每个键盘字符在该字符串中出现的次数,然后显示它。

即使满足条件,我正在运行的主循环也会读取字符,但不会运行。

您知道什么地方可能出问题吗?

 import java.util.Scanner;

public class mainClass {

public static void main (String[] args){

    Scanner myScanner = new Scanner(System.in);   
    System.out.println("Please enter array");  
    String inputString = myScanner.nextLine();    //input array entered
    myScanner.close();

            char[] charTypes = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,`,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},;,:};

    char[] charArray = new char[inputString.length()];


    for (int i =0; i<=inputString.length()-1;i++){   //conversion of string to character array

        charArray[i]= inputString.charAt(i);
        System.out.print(" "+ charArray[i] +" ");

    }


    int i1=0;
    int i2=0;
    int charCounter[] = new int[84];  //This array holds the amount of times each corresponding character occurs in a string

while (i1<=charArray.length-1){

    if(charArray[i1]==charTypes[i2] && i22<83 && i1<charArray.length-1){

        charCounter[i22]++;
        i1++;
        i22=0;

    }else if(charArray[i1] != charTypes[i2] && i22<83 && i1<charArray.length-1){

        i22++;

    }else{System.out.println("Not even running through the if's");//introduced as as tests to see if the conditions of the two if statements were being met}
}

for(int i = 0; i<=91;i++){ // prints out how many times each character occurs

    System.out.println(charTypes[i]+" : "+ charCounter[i]);

    }
}
}

一个问题是,就像在创建字符串时必须在字符串周围加上“”一样,必须在字符周围加上“”以使系统知道它是什么。

char[] charTypes = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F',
'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X',
'Y','Z','0','1','2','3','4','5','6','7','8','9','`','~','!','@','#','$',
'%','^','&','*','(',')','-','_','=','+','[','{',']','}',';',':'};

上面的代码中似乎有一个错字,您在多个地方将i2称为i22。

另一个问题是设置循环条件的方式。 目前:

if(charArray[i1]==charTypes[i2] && i2<83 && i1<charArray.length-1){

这样做的一个问题是,在检查以确保它们没有超出范围之前,您正在比较位置i1和i2处的数组中的元素。 如果将其更改为:

if(i2<83 && i1<charArray.length-1 && charArray[i1]==charTypes[i2]){

那么一旦发现i1或i2超出范围,它将退出测试,然后调用它们在数组中的位置并导致错误。

您可以使用字典/哈希图。 字典不仅可以用于计算字母,还可以用于统计单词和数据中的其他出现次数。 字符串inputString = myScanner.nextLine();

Map<char, Integer> list = new HashMap<char, Integer>();

for(char element: inputString){
    if(list.containsKey(c))//if char exist
    {
        list.put(element,list.get(element)+1);//increase value by 1
    }
    else{
        list.put(element,1);//add char with value 1
    }        
}

这应该为您提供字符串tex中所有字符计数的列表。 然后,您可以像这样打印它们。

for (Map.Entry<char, Integer> entry : list.entrySet()) {
    char key = entry.getKey();
    Integer value = entry.getValue();
    System.out.println(key+ " occurs " + value + " times");
}

暂无
暂无

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

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