Java - 将字符串解析为非原始数据类型文本文件为整数
Java - Parse a String to a non-primitive data type text file to an 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;
}
文本文件:
3::23::2019-09-23::2019-09-30::
错误:
Type mismatch: cannot convert from int to Patron
Type mismatch: cannot convert from int to Book
要完成您想要做的事情,您需要为 Book
和 Patron
创建一个构造函数
public class Book{
public Book(int i){
//what to do whith the int
}
}
然后你这样称呼它
Book book = new Book(Integer.parseInt(properties[1]));
我正在尝试将文本文件中的字符串转换为整数形式的非原始数据类型,但在尝试解析整数时出现错误
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;
}
文本文件:
3::23::2019-09-23::2019-09-30::
错误:
Type mismatch: cannot convert from int to Patron Type mismatch: cannot convert from int to Book
要完成您想要做的事情,您需要为 Book
和 Patron
public class Book{
public Book(int i){
//what to do whith the int
}
}
然后你这样称呼它
Book book = new Book(Integer.parseInt(properties[1]));