0

I'm having an extremely frustrating time trying to get the bug out of one of my programs. Basically my program takes in a string, and reads how many times each keyboard character occurs in the string, and then displays it.

The main loop I'm running which does the character reading, does not run through, even though the conditions are being met.

Do you have any idea what might be wrong?

 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]);

    }
}
}
demongolem
  • 9,474
  • 36
  • 90
  • 105

2 Answers2

0

One problem is that, just like you have to put "" around a String when you create it you have to put '' around a character to let the system know what it is.

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','`','~','!','@','#','$',
'%','^','&','*','(',')','-','_','=','+','[','{',']','}',';',':'};

There seems to be a typo in your code above where you refer to i2 as i22 in several places.

Another issue is in the way your loop conditions are set up. Currently:

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

One problem with this is that you're comparing the elements in the arrays at location i1 and i2 before you're checking to make sure they're not out of bounds. If you change it to:

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

then it will exit out of the test once it sees i1 or i2 are out bounds, before calling for their location in the array and causing an error.

Reagankm
  • 4,780
  • 9
  • 27
  • 52
0

You could use a dictionary/hashmap. dictionaries can be used to not only count letters but words and other occurrences in data. String 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
    }        
}

this should give you a list of all character counts in the string tex. Then you can print them like this.

for (Map.Entry<char, Integer> entry : list.entrySet()) {
    char key = entry.getKey();
    Integer value = entry.getValue();
    System.out.println(key+ " occurs " + value + " times");
}
Thomas Andreè Wang
  • 3,379
  • 6
  • 37
  • 53