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,564
  • 146
  • 81
  • 122
Mo Ok
  • 544
  • 6
  • 20

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,471
  • 1
  • 11
  • 19