# Printing out ... depending on character count of previous words entered.

• 11-20-2008, 07:27 AM
TheRocket
Printing out ... depending on character count of previous words entered.
Ok I have a program which allows the user to input two words, I then use the length() function to find out how many characters are in the words. Is it possible to add this two together and take it away from say 70 and then with the remaining character stick it between the two words.

Example:

First word = Hello

Second word = Goodbye

5+7 = 12
70 - 12 = 58

So it would output,

Hello............................................. .............Goodbye

The dots in the middle would vary depending on the length of the two words entered. I don't want to know how to code this just how I would work out how to calculate how many dot's would be needed and then how to print that out.

Thanks
• 11-21-2008, 06:18 AM
Eranga
What you have tried so far? You have to do few things.

1. How to get the user information, first word and last word
Hint: Use the Scanner class and netLine() method

2. How to calculate the length of the first word and the last and number of dots.
Hint: length() of the String class

3. How to display first word, dots required and the last word on a single line.
Hint: print(), not println()
• 11-21-2008, 08:09 AM
AnGuRuSO
Quote:

Originally Posted by TheRocket
I don't want to know how to code this just how I would work out how to calculate how many dot's would be needed and then how to print that out.

Well to calculate how many dots needed you would use code along the lines of:

int dotsRequires = 70 - ( word1.length() + word2.length());

To print out the dots you would need a while or for loop. Search up while and for loops in google and you should be able to print out required number of dots.
• 11-21-2008, 09:14 AM
Eranga
Quote:

Originally Posted by AnGuRuSO

int dotsRequires = 70 - ( word1.length() + word2.length());

To print out the dots you would need a while or for loop. Search up while and for loops in google and you should be able to print out required number of dots.

Yes, or else you can do the same thing without using extra variable. Something like this, pretty straight forward,

Code:

```        for(int i = 0; i < (70 - (first.length() + last.length())); i++) {             System.out.print(".");         }```
Put some effort ans try to answer my three questions, you'll have the solution.
• 11-21-2008, 02:25 PM
TheRocket
Thanks a lot guys, with a bit of trial and error from the above I got it :)
• 11-22-2008, 02:34 PM
Eranga