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;
}
``````

It looks like you are incrementing the `count` before checking whether `+` was entered.
I suggest you simplify your code and use the `while` loop instead of `do..while`.

``````import java.io.*;

public class Main {
public static void main(String[] args) throws Exception {

## All 4 Replies

It looks like you are incrementing the `count` before checking whether `+` was entered.
I suggest you simplify your code and use the `while` loop instead of `do..while`.

``````import java.io.*;

public class Main {
public static void main(String[] args) throws Exception {
int words = 0;
int chars = 0;
double avg = 0.0;
while (true) {
System.out.print("Enter next word: ");
if (word.equals("+"))
break;
words ++;
chars += word.length();
avg = (double)chars / (double)words;
System.out.println("Word count: " + words);
System.out.println("Average length: " + avg);
}
}
}
``````
commented: 1 +1

Does it make any difference if BufferedReader is not used for single words. After I posted the orginal thread I contined working on this code and I used the if statement after the while like this...

`````` while (!word.equals("*"))
{
word =  Dialog.request("enter next word");

if (word.equals("*"))
{
break;
}

count++;

numberOfCharacters = numberOfCharacters + word.length();
System.out.println(df.format(numberOfCharacters));
}
``````

but after seeing your code it makes sense. But what is the difference in what you provided and the I have just done?

Thanks sergb

It looks like there aren't many differences anymore. The code in your last post seems to be doing exactly what the code I posted was trying to do.

I used the BufferedReader for its readLine method because I was reading words from the stdin. If you have a different method of getting words--namely `Dialog.request()` )--then you don't need the BufferedReader at all.

It looks like you had it figured out before I posted the code.

thanks sergb, this has been solved. I will rate you if I am allowed...there is no function to rate you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.