0

so i am creating very simple function to calculate the probability of the repeated words but i found that the probability array are not being initialized ?? the probability always by 0 for example :

String original = "abcabd";
String n = "abcd";
int count = 0;
float Probabilty[] = new float[n.length()];

for( int i = 0 ; i < n.length() ; i++){
    for( int j =0 ; j < original.length() ; j++){
        if ( n.charAt(i)== original.charAt(j) ){
            count = count +1;
        }
    }
    Probabilty[i] = count/original.length();
    System.out.println(Probabilty[i]);
    count = 0;
}
Pang
  • 9,073
  • 146
  • 84
  • 117
Mohamed Salah
  • 506
  • 5
  • 14

1 Answers1

1

It has to do with the line:

Probabilty[i] = count/original.length();

Both count and original.length() are integers, so dividing the two uses integer division. If you convert one to a float or double then you'll get the proper division, like so:

Probabilty[i] = ((float)count)/original.length();

See How can I convert integer into float in Java? for a similar question.

Community
  • 1
  • 1
Federico Pettinella
  • 1,441
  • 1
  • 11
  • 19