sonu_chauhan
sonu_chauhan

Reputation: 335

Count the number of occurrences of all the digits in the string

My task is write a program to take string as input(only numbers) and for each digit starting from 0 to 9, print the count of their occurrences in the string. I have completed it. I have declared 10 integers with zero. Each integer will count the corresponding integers. But in the last when I am printing the result it is giving me the result as 48+count Count represents the number of count of values occurrences. For the correct result I need to subtract 48. I am unable to understand why I am getting value.

class TestClass {
public static void main(String args[] ) throws Exception { 
    Scanner sc = new Scanner(System.in);     
    int a='0',b='0',c='0',d='0',e='0',f='0',g='0',h='0',i='0',j='0';
    String s=sc.next();

    OUTER:
    for (int k = 0; k<s.length(); k++) {
        char ch=s.charAt(k);
        switch (ch) {
            case '0':
                a++;
                break;
            case '1':
                b++;
                break;
            case '2':
                c++;
                break;
            case '3':
                d++;
                break;
            case '4':
                e++;
                break;
            case '5':
                f++;
                break;
            case '6':
                g++;
                break;
            case '7':
                h++;
                break;
            case '8':
                i++;
                break;
            case '9':
                j++;
                break;
            case ' ':
                break OUTER;
            default:
                break;
        }
    }

   System.out.println("0 "+(a-48));
    System.out.println("1 "+(b-48));
     System.out.println("2 "+(c-48));
      System.out.println("3 "+(d-48));
       System.out.println("4 "+(e-48));
        System.out.println("5 "+(f-48));
         System.out.println("6 "+(g-48));
          System.out.println("7 "+(h-48));
           System.out.println("8 "+(i-48));
            System.out.println("9 "+(j-48));


}
}

please anyone explain me what I can do for removing this extra value in this program. thanks

Upvotes: 0

Views: 2922

Answers (2)

Ankit Deshpande
Ankit Deshpande

Reputation: 3604

I would suggest to use an array instead. it'll be easier to process.

String str = sc.next();
char[] input = str.toCharArray();

int[] count = new int[10]; // stores the count, int array initialized to 0 by default

for(int i = 0 ; i < input.length; i++){
    // get index value by substracting ASCII value
    int c = input[i] - 48; // 48 being ASCII Value of '0'
    count[c]++;
}

// print the count array
System.out.println(Arrays.toString(count));

count[0] has no of 0's
count[1] has no of 1's
.....

Upvotes: 1

smttsp
smttsp

Reputation: 4191

Instead of

int a = '0' 

use

int a = 0

'0' is equal to 48 in ASCII and it is a character, not a number. So by int a = '0', you actually initialize a to 48

Upvotes: 4

Related Questions