I'm trying to convert the string from the text file into a non-primitive data type as an integer but i get an error when i try to parse integer
try (Scanner sc = new Scanner(new File(RESOURCE))) {
int line_idx = 1;
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] properties = line.split(SEPARATOR, -1);
try {
Patron patron = Integer.parseInt(properties[0]);
Book book = Integer.parseInt(properties[1]);
LocalDate startDate = LocalDate.parse(properties[2]);
LocalDate dueDate = LocalDate.parse(properties[3]);
Loan loan = new Loan(patron, book, startDate, dueDate);
Book.setLoan(loan);
Patron.addBook(book);
} catch (NumberFormatException ex) {
throw new LibraryException("Unable to parse patron id "
+ properties[0] + " on line " + line_idx + "\nError: " + ex);
}
line_idx++;
}
}
//Loan file
private Patron patron;
private Book book;
private LocalDate startDate;
private LocalDate dueDate;
public Loan(Patron patron, Book book, LocalDate startDate, LocalDate dueDate) {
this.patron = patron;
this.book = book;
this.startDate = startDate;
this.dueDate = dueDate;
}
public Book(int id, String title, String author, String publicationYear, String publisher) {
this.id = id;
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
this.publisher = publisher;
}
Text file:
3::23::2019-09-23::2019-09-30::
Error:
Type mismatch: cannot convert from int to Patron Type mismatch: cannot convert from int to Book
To do what you're trying to do you need to make a constructor for Book
and Patron
public class Book{
public Book(int i){
//what to do whith the int
}
}
and then you call it like this
Book book = new Book(Integer.parseInt(properties[1]));