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

1. Member
Join Date
Nov 2008
Posts
20
Rep Power
0

## 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

2. 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()

3. Member
Join Date
Oct 2008
Posts
9
Rep Power
0
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.

4. 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,

Java 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.

5. Member
Join Date
Nov 2008
Posts
20
Rep Power
0
Thanks a lot guys, with a bit of trial and error from the above I got it :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•