无法将自定义数据类型转换为字符串?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法将自定义数据类型转换为字符串?相关的知识,希望对你有一定的参考价值。
我正在使用java序列化开发一个库系统。当我实现书籍问题部分时,我需要存储书籍的细节和成员的详细信息。
为此,我使用了“issueNewBook”方法。该方法属于“Book”类。所以当我调用此方法时,它有一个属性“libraryMember member”,这将返回成员id。
在我调用此方法之后,它表示字符串无法转换为libraryMember?我不知道如何解决这个问题。我尝试了所有我知道的事情。你的帮助在这个时候非常重要。谢谢。
图书馆成员班:
public class libraryMember implements Serializable{
String userType;
String username;
String password;
String confirmPassword;
private String name;
public int memberNumber;
private SetOfBooks currentLoans = new SetOfBooks();
private static int memberCount = 0;
public libraryMember() {
userType=null;
username=null;
password=null;
confirmPassword=null;
}
public libraryMember(String aName){
name= aName;
memberNumber = memberCount++;
currentLoans = new SetOfBooks();
}
@Override
public String toString(){
return Integer.toString(memberNumber) + " " + name;
}
public void borrowBook(Book aBook){
currentLoans.addBook(aBook);
aBook.setBorrower(this);
}
public String getUserType(){
return this.userType;
}
public Object getName() {
return this.username;
}
public String getPassword() {
return this.password;
}
public String getConfirmPassword() {
return this.confirmPassword;
}
public void singUp(Object userType, String username, String password) {
this.userType = (String) userType;
this.username = username;
this.password = password;
}
}
预订课程:
public class Book implements Serializable{
public String title;
public String author;
public String publisher;
public String isbn;
public int noOfCopies;
private libraryMember borrower = null;
private static int bookCount = 0;
private int accessionNumber;
public String Issuedate;
public String returnDate;
public Book(String name){
title = name;
accessionNumber = bookCount++;
}
public void addNewBook(int bookID, String title, String author,String publisher,String isbn,int noOfCopies) {
this.accessionNumber = bookID;
this.title = title;
this.author = author;
this.publisher = publisher;
this.isbn=isbn;
this.noOfCopies=noOfCopies;
}
public void issueNewBook(int bookID,String title,libraryMember member,String issuedDate,String returnDate)
{
this.accessionNumber= bookID;
this.title= title;
this.borrower=member;
this.Issuedate = issuedDate;
this.returnDate=returnDate;
}
public int getBookID()
{
return accessionNumber+1;
}
public String getBookName()
{
return title;
}
void setBorrower(libraryMember theBorrower){
borrower = theBorrower;
}
libraryMember geMember(){
return borrower;
}
}
书籍问题按钮动作:
// TODO在这里添加您的处理代码:
ArrayList<Book> bookList = new ArrayList();
int flag = 0;
try {
FileInputStream fis = new FileInputStream("issuedBook.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Book book;
while ((book = (Book) ois.readObject()) != null) {
if (book.getBookName().equals(issue_BookNameArea.getText())) {
flag = 1;
break;
}
bookList.add(book);
}
ois.close();
fis.close();
}
catch(FileNotFoundException e)
{
}
catch (EOFException e) {
} catch (ClassNotFoundException ex2) {
} catch (IOException e) {
}
if (flag == 0) {
//write to file only if name doesn't exist
Book bk = new Book(getName());
String s = issue_BookNameArea.getText();
libraryMember member = null;
bk.issueNewBook((Integer.parseInt(issue_BookIdField.getText())),issue_BookNameArea.getText(),issue_MemberID.getText(), issueDate_dateChooser.getText(), returndaateField.getText());
bookList.add(bk);
try {
FileOutputStream fos;
ObjectOutputStream oos;
fos = new FileOutputStream("newMember.dat");
oos = new ObjectOutputStream(fos);
Book b;
Iterator i = bookList.iterator();
while (i.hasNext()) {
b = (Book) i.next();
oos.writeObject(b);
oos.flush();
}
fos.close();
oos.close();
// dispose();
} catch (IOException e) {
}
}
}
问题包含方法:
bk.issueNewBook((Integer.parseInt(issue_BookIdField.getText())),issue_BookNameArea.getText(),issue_MemberID.getText(),issueDate_dateChooser.getText(),returndaateField.getText());
错误来自:
issue_MemberID.getText()
错误:
字符串无法转换为libraryMember
代替:
bk.issueNewBook((Integer.parseInt(issue_BookIdField.getText())),issue_BookNameArea.getText(),issue_MemberID.getText(), issueDate_dateChooser.getText(), returndaateField.getText());
尝试使用libraryMember
中的字符串创建一个新的getText()
对象(因为您已经定义了一个带有字符串的构造函数)并将其作为第三个参数传递:
bk.issueNewBook((Integer.parseInt(issue_BookIdField.getText())),issue_BookNameArea.getText(),new libraryMember(issue_MemberID.getText()), issueDate_dateChooser.getText(), returndaateField.getText());
以上是关于无法将自定义数据类型转换为字符串?的主要内容,如果未能解决你的问题,请参考以下文章
C# 使用DateTime.TryParseExact将自定义日期类型转换成日期
C++ 将自定义类型的转换运算符重载为 std::string