aspose file tools*
The moose likes Beginning Java and the fly likes while loop program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "while loop program" Watch "while loop program" New topic
Author

while loop program

Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
I have to do the following assignment:

Write a program that asks the user to enter two words. The program then prints out both words on one line. The words will be separated by enought dots so that the total line length is 30:

Enter first word:
turtle
Enter second word
153

turtle....................153

This could be used as part of an index for a book. To print out the dots, use System.out.print(".") inside a loop body.

and I know to use a while loop, but not much else. This is what I have:



*********************

I know I need to use inputString.length() I just don't know where.


Thank you!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

So you can get the two lengths, right? And you know you need 30 characters total.

What's the next step?

(Btw, please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.)
Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
So, I get the lengths of firstWord and secondWord, and then put that into a variable? then, while ((firstWord + secondWord) < 30) count = count + 1 and then print the number of ....'s for counts?

Thanks!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Theresa Marlin wrote: . . ., while ((firstWord + secondWord) < 30) count = count + 1 and then print the number of ....'s for counts?
That is one way to do it. You will get better performance with a StringBuilder and adding one . at a time, because adding to a StringBuilder is very fast and printing is slow.
Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
Thank you, but since I do not know StringBuilder yet, I should probably stick with what I do know.
Thank you all for your help!!!
 
jQuery in Action, 2nd edition
 
subject: while loop program