需要帮助创建一个从书中打印目录的java程序 [英] Need help creating a java program that prints a table of contents from a book
问题描述
我应该编写一个java程序,它使用以下数据结构打印目录:
I'm supposed to write a java program, which prints a table of contents, using the following data structure:
public class TocEntry
{
// Specify the needed methods
private String chapter;
private int page;
}
以下在我的驱动程序类中定义:
And the following defined in my driver class:
public final int TOCSIZE = 100;
TocEntry toc[] = new TocEntry[TOCSIZE];
int toc_curlen = 0; //The toc_curlen is intended to keep track of the number of chapters entered by the user and it can be used as an index into the array of TocEntry objects.
接下来,我应该在我的TocEntry类中开发必要的代码来读取输入 **
之前的章节名称和页码。从这里,我的输出应该如下所示:
Next, I'm supposed to develop the necessary code, in my TocEntry class, to read in a chapter name and page number until "**
" is entered. From this, my output should look like this:
我的故事开始............... ........ 1
My Story Starts.......................1
成长........................ ... 35
Growing up...........................35
征服世界........ 103
Conquering the World........103
这应该是我使用名为useTocEntry的驱动程序运行的示例
This is supposed to be my sample run using a driver called useTocEntry
输入章节标题:Camelot
Enter chapter title: Camelot
输入起始页码:1
输入章节标题:King Arthur's Court
Enter chapter title: King Arthur's Court
输入起始页码:3
输入章节标题:桌轮骑士
Enter chapter title: Knights of the Table Round
输入起始页数字:8
输入章节标题:幽默作家迪纳丹爵士
Enter chapter title: Sir Dinadan the Humorist
输入起始页数:12
输入章节标题:灵感
输入起始页码:14
输入章节标题:Ec lipse
Enter chapter title: The Eclipse
输入起始页码:23
输入章节标题:克拉伦斯的后记
Enter chapter title: A Postscript by Clarence
输入起始页码:274
Enter starting page number: 274
输入章节标题: **
Camelot .................................. ........ 1
Camelot..........................................1
亚瑟王的法庭....................... ..3
King Arthur's Court.........................3
桌轮骑士............ 8
Knights of the Table Round............8
Sir Dinanan the Humorist .............. 12
Sir Dinadan the Humorist..............12
灵感............. ................... 14
An Inspiration................................14
Eclipse ............. ....................... 23
The Eclipse...................................23
克拉伦斯的后记........ ..... 274
A Postscript by Clarence.............274
这是我到目前为止的代码:
This is the code I have so far:
import java.util.*;
public class TocEntry {
public TocEntry() { // Default Constructor
chapter = "";
page = 0;
}
public TocEntry(String c, int p) { // 2 Argument Constructor
chapter = c;
page = p;
}
public String getChapter() { //getChapter() and getPage() are accessor methods
return chapter;
}
public int getPage() {
return page;
}
public void setChapter(String title) { ////setChapter() and setPage() are mutator methods
chapter = title;
}
public void setPage(int numPage) {
page = numPage;
}
private String chapter;
private int page;
public String toString() { // toString method to print out contents
return chapter + "**" + page;
}
}// End of class TocEntry
这是我的驱动程序class:
And this is my driver class:
import java.util.Scanner;
public class useToEntry {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of contents you would like to display: ");
int TOC_NUM = input.nextInt();
final int TOCSIZE = 100;
TocEntry toc[] = new TocEntry[TOCSIZE];
int toc_curlen = 0; // The toc_curlen is intended to keep track of the number
// of chapters entered by the user and it can be used as an
// index into the array of TocEntry objects.
for(int i = 0; i < TOC_NUM; i++) {
System.out.print("Enter chapter title: ");
String ch = input.next();
System.out.print("Enter starting page number: ");
int y = input.nextInt();
}// End of for loop
} // End of main method
}// End of class useTocEntry
我需要帮助,尤其是创建一个方法,将 **
与章节标题和页面对齐数。我的代码将获得任何帮助或建议。
I need help, especially to create a method that aligns the **
with the chapter title and the page number. Any help or advice on my code would be appreciated.
推荐答案
所以。您需要知道的最大可能字符串
所有章节的长度。
So. What you need to know the maximum possible String
length of all the chapters.
这是最容易实现的监控在输入阶段输入了文字...类似于......
This is easiest achieved by monitoring the text been entered during your input stage...something like...
int maxChapterLength = 0;
for(int i = 0; i < TOC_NUM; i++) {
System.out.print("Enter chapter title: ");
String ch = input.next();
maxChapterLength = Math.max(ch.length(), maxChapterLength);
System.out.print("Enter starting page number: ");
int y = input.nextInt();
}// End of for loop
然后,您可以确定所需的填充章节名称和章节号码之间
From this, you can then determine the required padding between the chapter name and the chapter number
类似......
public static String pad(String sValue, int iMinLength) {
StringBuilder sb = new StringBuilder(iMinLength);
sb.append(sValue);
while (sb.length() < iMinLength) {
sb.append(".");
}
return sb.toString();
}
从那里你只需要将信息发送到屏幕...类似......
From there you simply need to send the info to the screen...something like...
System.out.println(pad(toc[index].getChapter(), maxChapterLength + 2) + toc[index].getPage());
现在,看了你的代码,你还有其他一些问题,但我离开了它是你要识别的。
Now, having looked at you're code, you have a bunch of other problems, but I leave it to you to identify.
这篇关于需要帮助创建一个从书中打印目录的java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!