File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

while loop program

 
Theresa Marlin
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic