在Java中创建对象时无法传递任何参数

时间:2016-05-26 16:21:49

标签: java bluej

我试图创建"借款人"的对象。类。我尝试了所有参数组合,但它给了我一切错误。不确定发生了什么。它的设置方式是String,int,int但是当我以这种格式给出它时,它给了我错误。

以下是参数屏幕的图像: enter image description here

借款人类的代码是:

import java.util.ArrayList;

public class Borrower {
    private String name;
    private int id;
    private int age;
    private ArrayList<Book> booklist;

    public Borrower(String[] info) {
        this.setName(info[0]);
        this.setId(Integer.parseInt(info[1]));
        this.setAge(Integer.parseInt(info[2]));

        this.booklist = new ArrayList<Book>();
        if(info.length == 5) { 
            this.booklist.add(new Book(info[3], info[4], ""));
        } else if(info.length == 6) { 
            this.booklist.add(new Book(info[3], info[4], info[5]));
        } else if(info.length == 7) { 
            this.booklist.add(new Book(info[3], info[4], ""));
            this.booklist.add(new Book(info[5], info[6], ""));
        } else if(info.length == 8) {
            this.booklist.add(new Book(info[3], info[4], info[5]));
            this.booklist.add(new Book(info[6], info[7], info[8]));
        }
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public ArrayList<Book> getBooklist() {
        return booklist;
    }
    public boolean addBook(Book book) {
        if(this.booklist.size() < 2) {
            this.booklist.add(book);
            return true;
        }
        return false;
    }

    public boolean returnBook() {
        if(this.booklist.size() > 0) {
            this.booklist.remove(0);
            return true;
        }
        return false;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.getName());
        sb.append("," + this.getId());
        sb.append("," + this.getAge());
        for(Book book : booklist) {
            sb.append("," + book);
        }
        return sb.toString();
    }
}

感谢您的任何帮助。

更新:错误截图: enter image description here

1 个答案:

答案 0 :(得分:1)

以这种方式写下

{"Arvind" , "1" , "2"}

在对话框中输入以上内容。 基本上{}是缺少的标识符。