Hello,
the following code is not exactly working properly. Bascally when I enter + in the dialog box it escape but still increments in the calculation. So for instance if I first enter a word "hello" then I click OK button then I enter word "there" then I click OK button then I enter + in the dialog box, it will exist the loop but the calcualation still includes the +, as shown shown below
hello has 5 characters
there has 5 characters
+ includes so that is 1 characters
so in total the it adds to 11, divide by count which gives final answer 3.6666666666666665
when it should be
hello has 5 characters
there has 5 characters
so in total it should be in total 10, divide by count which the final answer should be 5.
Here is my code and I hope I have format the thread correctly. Thanks in advance
public static double average()
{
int count = 0; // number of words input
double Characterscounting = 0; // total number of characters
String word = ""; // to hold each word as it is input
DecimalFormat df = new DecimalFormat("#.#");
double avg;
do {
word = Dialog.request(" enter next word");
count++;
numberOfCharacters = numberOfCharacters + word.length();
System.out.println(df.format(numberOfCharacters));
avg = numberOfCharacters /count;
// System.out.println(df.format(avg));
} while (!word.equals("+")); // i think here is the problem
// but don't know who to fix it
if (word=="*") // obviously this does not work
{
count = count - 1;
}
return avg;
}