Как вы можете видеть в конце кода, я использую "list.printList", который вызывает метод printLink. Вместо этого он должен вывести «имя книги: ... автор книги: .. и т. Д.», Он показывает необработанные данные текстового файла. Что я делаю неправильно? Также любые рекомендации приветствуются. спасибо
РЕДАКТИРОВАТЬ: это содержимое текстового файла:
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);
**/
}
}