I would like to read from a file which the user can choose on start of program. I want to read line for line, checking if the format of each is ok. if it is , I add it to ArrayList of good books, if not I add it to ArrayList of error books. The date isnt parsing correctly.
import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Book {
String isbn;
int copyNumber;
String title;
String author;
int statistics;
Date borrowDate = new Date();
Date returnDate = new Date();
int libraryCardNumber;
DateFormat formatter = new SimpleDateFormat("yy/mm/dd");
public Book(String newIsbn, int newCopyNumber, String newTitle, String newAuthor,
int newStatistics, Date newBorrowDate, Date newReturnDate, int newLibraryCardNumber)
{
isbn = newIsbn;
copyNumber = newCopyNumber;
title = newTitle;
author = newAuthor;
statistics = newStatistics;
borrowDate = newBorrowDate;
returnDate = newReturnDate;
libraryCardNumber = newLibraryCardNumber;
}
public Book(){
}
public Book(String newRow) throws ParseException
{
String[] token = newRow.split("#");
isbn = token[0];
copyNumber = Integer.parseInt(token[1]);
title = token[2];
author = token[3];
statistics = Integer.parseInt(token[4]);
borrowDate = (Date)formatter.parse(token[5]);
returnDate = (Date)formatter.parse(token[6]);
libraryCardNumber = Integer.parseInt(token[7]);
}
public String getIsbn()
{
return isbn;
}
public void setIsbn(String isbn)
{
this.isbn = isbn;
}
public int getCopyNumber()
{
return copyNumber;
}
public void setCopyNumber(int copyNumber)
{
this.copyNumber = copyNumber;
}
public String getTitle()
{
return title;
}
private void setTitle(String title)
{
this.title = title;
}
public String getAuthor()
{
return author;
}
private void setAuthor(String author)
{
this.author = author;
}
public int getStatistics()
{
return statistics;
}
public void setStatistics(int statistics)
{
this.statistics = statistics;
}
public Date setBorrowDate()
{
return borrowDate;
}
public void getBorrowDate(Date borrowDate)
{
this.borrowDate = borrowDate;
}
public Date getReturnDate()
{
return returnDate;
}
public void setReturnDate (Date returnDate)
{
this.returnDate = returnDate;
}
public int getLibraryCardNumber()
{
return libraryCardNumber;
}
public void setLibraryNumber(int libraryCardNumber)
{
this.libraryCardNumber = libraryCardNumber;
}
}
import java.io.*;
import java.text.ParseException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFileChooser;
public class WriteFromBookFile {
private static Pattern pattern;
public static void main(String[] args) throws FileNotFoundException, ParseException, IOException {
JFileChooser fileChooser = new JFileChooser();
if(fileChooser.showOpenDialog(null)
== JFileChooser.APPROVE_OPTION){
ArrayList<Book> newBooks = new ArrayList<Book>();
ArrayList<Book> errorBooks = new ArrayList<Book>();
java.io.File file = fileChooser.getSelectedFile();
// try{ BufferedReader reader = new BufferedReader(new FileReader("books.txt"));
Scanner input = new Scanner(file);
String line = input.nextLine();
while (line != null) {
Book newBook = new Book(line);
String[] newToken = line.split("#");
pattern = Pattern.compile("ISBN\\x20(?=.{13}$)\\d{1,5}([- ])\\d{1,7}\\1\\d{1,6}\\1(\\d|X)$");
Matcher matchIsbn = pattern.matcher(newToken[0]);
pattern = Pattern.compile("[\\d]+");
Matcher matchCopyNumber = pattern.matcher(newToken[1]);
pattern = Pattern.compile("[\\D&&\\w]+");
Matcher matchTitle = pattern.matcher(newToken[2]);
pattern = Pattern.compile("[\\D&&\\w]+");
Matcher matchAuthor = pattern.matcher(newToken[3]);
pattern = Pattern.compile("[\\d]+");
Matcher matchStatistics = pattern.matcher(newToken[4]);
pattern = Pattern.compile("YY/MM/DD");
Matcher matchBorrowDate = pattern.matcher(newToken[5]);
pattern = Pattern.compile("YY/MM/DD");
Matcher matchReturnDate = pattern.matcher(newToken[6]);
pattern = Pattern.compile("[\\d]+");
Matcher matchLibraryCardNumber = pattern.matcher(newToken[7]);
/* if(matchIsbn.matches(newBook.getIsbn()) && matchCopyNumber.matches(newBook.getCopyNumber())
&& matchTitle.matches(newBook.getTitle()) && matchAuthor.matches(newBook.getAuthor())
&& matchStatistics.matches(newBook.getStatistics()) && matchBorrowDate.matches(newBook.getBorrowDate())
&& matchReturnDate.matches(newBook.getReturnDate()) && matchLibraryCardNumber.matches(newBook.getLibraryCardNumber()));*/
if (matchIsbn.matches() && matchCopyNumber.matches() && matchTitle.matches() && matchAuthor.matches()
&& matchStatistics.matches() && matchBorrowDate.matches() && matchReturnDate.matches() && matchLibraryCardNumber.matches())
{
newBooks.add(newBook);
}
else
errorBooks.add(newBook);
line = input.nextLine();
}
input.close();
java.io.File newBookFile = new java.io.File("newBook.txt");
if(newBookFile.exists()){
System.out.println("File exists.");
System.exit(0);
}
java.io.PrintWriter output = new java.io.PrintWriter(newBookFile);
for(int i = 0; i < newBooks.size(); i++)
{
output.print(newBooks.get(i));
}
output.close();
}
}
}