Question:
Java Program Help (loops)?
2011-12-05 18:24:45 UTC
Been on this one for hours and now it is due in an hour... PLZ HELP?
I cant seem to get the right commands into the program..

Program Description: A table of contents in many books use periods to align text with the page numbers. These are called “dot leaders”. You are to write a program that accepts from the user a chapter title and a page number. Then you will output these to a line that contains exactly fifty characters. The space between the title and the page number will be filled with periods (“.”).

Statements Required: input, output, loop control, strings, and string methods

Sample Output:

Enter the title: An Introduction to Java
Enter the page number: 5

An Introduction to Java……………………….5

Enter the title: Simple Data Types
Enter the page number: 27



Simple Data Types………………………..…27

THANKS IN ADVANCE
Three answers:
McFate
2011-12-05 19:13:30 UTC
Think about how many periods you want to print: 50, minus the length of your title (which will be a String, so it has a length() method that will tell you), minus the length of your page number (which can also be a String).



String title = myScanner.nextLine();

Stirng pageNo = myScanner.nextLine();



Then the periods would be:



for (int i=0 ; i<50-title.length()-pageNo.length(); ++i) System.out.print(".");
CompuTecky
2011-12-06 02:54:33 UTC
here is a basic layout:



-get length of title.

-get length of number (5=1,27=2)

-number of periods = 50-both of those lengths added together
larrybud2004
2011-12-06 02:26:06 UTC
We're not going to write your code for you. Post what you have already done and where you're stuck.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...