Java; Initializing A Count of Input, After Period Is Entered

Your count variable is named Q. You increment it on each loop in the increment statement Q = Z + y

Where you have Z = text.length(); and y = user.length();

Now, since you don’t add those value to the previous Q value, you simply have Z + y so you only get the length of user and the last text, not the total count (correct me if I am wrong on the needs)

Use Q = Q + Z + y or Q += Z + y to increment correctly. (even if this should be done in the block statements, not in the for-increment part.

Note :

  • Use better variable name : a one letter variable is usually restricted for a index like int i
  • A while loop would fit better this logic : even if your loop is correct, a for loop is usually use to iterate a collection, not to hide some logic in it.
  • if user.endsWith(".") == true, you will never prompt anything (correct)

A simpler version that should do the same if I get your code.

System.out.println ("Enter Charecters:");
int count = 0;
String text;
    text = stdin.readLine();
    count += text.length;
} while(!text.endsWith("."));

Browse More Popular Posts

Leave a Comment