正如您在代码末尾看到的那样,我使用调用printLink方法的“list.printList”。它应该输出“书名:...书籍作者:......等”,它显示原始文本文件数据。我究竟做错了什么?此外,欢迎任何建议。感谢
编辑:这是文本文件的内容:
antrea ggfg 65 64
antrea ggfg 65 64
antrea ggfg 65 64
antrea ggfg 65 64
这是输出:
antrea ggfg 65 64
antrea ggfg 65 64
antrea ggfg 65 64
antrea ggfg 65 64
另外我认为令牌化器有问题。根据您的经验,我们应该如下所述做什么?
import java.io.* ;
import java.util.* ;
class Link
{
public String isbn ;
public String bookName ;
public String bookAuthor ;
public String publicYear;
public Link nextLink;
public Link ()
{
}
//Link constructor
public Link(String bookNameIn, String bookAuthorIn, String isbnIn, String publicYearIn)
{
setIsbn (isbnIn) ;
setBookName (bookNameIn) ;
setBookAuthor (bookAuthorIn) ;
setPublicYear (publicYearIn) ;
//isbn = isbnIn ;
//bookAuthor = bookAuthorIn ;
//publicYear = publicYearIn ;
}
// Set Methods
public void setIsbn(String isbnIn)
{
isbn = isbnIn ;
}
public void setBookName(String bookNameIn)
{
bookName = bookNameIn ;
}
public void setBookAuthor(String bookAuthorIn)
{
bookAuthor = bookAuthorIn ;
}
public void setPublicYear(String publicYearIn)
{
publicYear = publicYearIn ;
}
// Get Methods
public String getIsbn()
{
return isbn ;
}
public String getBookName()
{
return bookName;
}
public String getBookAuthor()
{
return bookAuthor ;
}
public String getPublicYear()
{
return publicYear ;
}
//Print Link data
public void printLink()
{
System.out.print("Book Name: " + getBookName() + "\n" + "Book's Author: " + getBookAuthor() + "\n" + "Year Published: " + getPublicYear() + "\n" + "ISBN: " + getIsbn() +"\n");
System.out.println("") ;
}
}
class LinkList
{
private Link first;
//LinkList constructor
public LinkList()
{
first = null;
}
//Returns true if list is empty
public boolean isEmpty()
{
return first == null;
}
//Inserts a new Link at the first of the list
public void insert(String bookNameIn, String bookAuthorIn, String isbnIn, String publicYearIn)
{
Link link = new Link(bookNameIn, bookAuthorIn, isbnIn, publicYearIn) ;
link.nextLink = first;
first = link;
}
//Deletes the link at the first of the list
public Link delete()
{
Link temp = first;
first = first.nextLink;
return temp;
}
//Prints list data
public void printList()
{
Link currentLink = new Link() ;
currentLink = first;
while(currentLink != null) {
currentLink.printLink();
currentLink = currentLink.nextLink;
}
System.out.println("");
}
}
public class TheList
{
public static void main(String[] args) throws Exception
{
//Scanner kb = new Scanner (System.in);
LinkList list = new LinkList();
File outputFile ;
//int numberOfBooks = 0 ;
// Create the new text file. If exists, it will continue to the next commands
do
{
outputFile = new File("db.txt") ;
if(!outputFile.exists())
{
outputFile.createNewFile ();
System.out.println("The file was created as db.txt");
System.out.println("");
}
}while (!outputFile.exists()) ;
try
{
// Define which file to stream in from
FileInputStream fileIn = new FileInputStream("db.txt") ;
DataInputStream input = new DataInputStream (fileIn) ;
BufferedReader br = new BufferedReader (new InputStreamReader (input)) ;
String line ;
// Read each line of the file
while ((line = br.readLine()) != null)
{
// insert code to break input to pieces
StringTokenizer tokenizer = new StringTokenizer(line) ;
while (tokenizer.hasMoreElements())
{
Link record = new Link() ;
record.setBookName(tokenizer.nextToken()) ;
record.setBookAuthor(tokenizer.nextToken()) ;
record.setIsbn(tokenizer.nextToken()) ;
record.setPublicYear(tokenizer.nextToken()) ;
}
System.out.println (line) ;
}
input.close() ;
}catch (Exception e){
System.err.println("Error. Could not read the file") ;
}
list.printList();
/**list.insert(123456, "kostis", "kostis", 1900);
list.insert(123456, "kostis", "kostis", 1900);
list.insert(123456, "kostis", "kostis", 1900);
list.insert(123456, "kostis", "kostis", 1900);
list.insert(123456, "kostis", "kostis", 1900);
**/
}
}
答案 0 :(得分:0)
我没有看到你在列表中添加任何内容,因此我假设printList()
方法只打印一个空行。
原始文本文件数据似乎来自这一行:
System.out.println (line) ;
我认为你希望你的阅读代码是这样的(虽然我怀疑内在的同时会/应该是必要的):
while ((line = br.readLine()) != null)
{
// insert code to break input to pieces
StringTokenizer tokenizer = new StringTokenizer(line) ;
while (tokenizer.hasMoreElements())
{
String bookName = tokenizer.nextToken();
String author = tokenizer.nextToken();
String isbn = tokenizer.nextToken();
String year = tokenizer.nextToken();
list.insert(bookName, author, isbn, year) ;
}
System.out.println (line) ;
}
此外,这似乎很奇怪:
Link currentLink = new Link() ;
currentLink = first;
为什么不只是以下?
Link currentLink = first;
还有其他风格问题,但我暂时没有对这些问题发表评论。
答案 1 :(得分:0)
你刚刚打印
System.out.println (line) ;
您永远不会将记录添加到列表中,因此行
list.printList();
什么都不做。 在while循环中,您应该将记录添加到列表中。
以下内容:
while (tokenizer.hasMoreElements())
{
Link record = new Link() ;
record.setBookName(tokenizer.nextToken()) ;
record.setBookAuthor(tokenizer.nextToken()) ;
record.setIsbn(tokenizer.nextToken()) ;
record.setPublicYear(tokenizer.nextToken()) ;
}
应该是:
while (tokenizer.hasMoreElements())
{
list.insert(values go here);
}
答案 2 :(得分:0)
你的问题在这里:
while ((line = br.readLine()) != null)
{
// insert code to break input to pieces
StringTokenizer tokenizer = new StringTokenizer(line) ;
while (tokenizer.hasMoreElements())
{
Link record = new Link() ;
record.setBookName(tokenizer.nextToken()) ;
record.setBookAuthor(tokenizer.nextToken()) ;
record.setIsbn(tokenizer.nextToken()) ;
record.setPublicYear(tokenizer.nextToken()) ;
}
System.out.println (line) ;
}
您正在为每条记录创建new Link()
,填充它,从不将其添加到列表中。它打印原始数据,因为那里有System.out.println(line);
。