当前位置: 首页 > 知识库问答 >
问题:

如何使用用户输入生成对象?

夏侯昊明
2023-03-14

下面列出的代码不允许创建新书对象,即使有构造函数也不行。我得到了一个NullPointerException,我想这是因为我在初始化对象时出错了。最终目标是创建一个程序,询问用户是否要创建新的图书对象,当用户说“是”时,它会返回并创建另一个图书对象。

public static void main(String args[]) {
        BookstoreBook arrOfBBooks[] = new BookstoreBook[100];
        LibraryBook[] arrOfLBooks = new LibraryBook[200];
        Scanner uinput = new Scanner(System.in);

        int counter = 0;
        do {


            System.out.println("Welcome to the book program!");
            System.out.println("Would you like to create a book object?");
            String creatingAnObjQ = uinput.nextLine();
            creatingAnObjQ = creatingAnObjQ.toUpperCase();
            if (creatingAnObjQ.equals("YES")) {
                System.out.println("Please enter the name of the author, title of the book, and isbn separated by / :");
                String ATI = uinput.nextLine();
                String[] strsOfATI = ATI.split("/");
                System.out.println("Got it!");
                System.out.println("Now, tell me if it is a bookstore book or a library book(enter BB for bookstore book or LB for library book): ");
                String typeOfBook = uinput.nextLine();
                typeOfBook = typeOfBook.toUpperCase();
                //while (!typeOfBook.equals("BB") || !typeOfBook.equals("LB")) {
                //    System.out.println("Invalid entry, try again");
                //   typeOfBook = uinput.nextLine();
                //}
                if (typeOfBook.equals("BB")) {
                    System.out.println("Got it!");
                    //arrOfBBooks[0] = new BookstoreBook("Aj", "Atomic", "123")
                    arrOfBBooks[counter].setAuthor(strsOfATI[0]);
                    arrOfBBooks[counter].setTitle(strsOfATI[1]);
                    arrOfBBooks[counter].setIsbn(strsOfATI[2]);
                    System.out.println("Please enter the price of" + arrOfBBooks[counter].getTitle() + "by" + arrOfBBooks[counter].getAuthor());
                    int priceOfBBook = uinput.nextInt();
                    System.out.println("Is it on sale? (yes/no): ");
                    String ifOnSale = uinput.nextLine();
                    int finalAmountOfSale = 0;
                    int finalPrice = 0;
                    if (ifOnSale.equals("yes")) {
                        System.out.println("Deduction percentage: ");
                        String preAmountOfSale = uinput.nextLine();
                        String[] breakingPercent = preAmountOfSale.split("%");
                        finalAmountOfSale = Integer.parseInt(breakingPercent[0]);
                        finalPrice = (finalAmountOfSale / 100) + priceOfBBook;
                        System.out.println("Got it!");
                    }
                    //arrOfBBooks[counter] book = new BookstoreBook(strsOfATI[0], strsOfATI[1], strsOfATI[2], priceOfBBook, finalPrice);
                    arrOfBBooks[counter].setPrice(priceOfBBook);
                    arrOfBBooks[counter].setFinalPrice(finalPrice);
                    System.out.println("Here is your bookstore ");
                    System.out.println("[" + arrOfBBooks[counter].getIsbn() + "-" + arrOfBBooks[counter].getTitle().toUpperCase() + "by" + arrOfBBooks[counter].getAuthor().toUpperCase() + ", $" + arrOfBBooks[counter].getPrice() + "listed for $" + arrOfBBooks[counter].getFinalPrice());
                }
                //end of BB if loop

            }   //end of saying "Yes" to creating an object


        } while (true);
    }


    class BookstoreBook {

        private String author;
        private String title;
        private String isbn;
        //int percentOff;
        private int price;

        private int finalPrice;


    public BookstoreBook(){

    }
    public BookstoreBook(String author, String title, String isbn){
        this.author = author;
        this.title = title;
        this.isbn = isbn;
    }
    public BookstoreBook(String author, String title, String isbn, int price, int finalPrice) {
        this.author = author;
        this.title = title;
        this.isbn = isbn;
        this.price = price;
        this.finalPrice = finalPrice;
    }

    public BookstoreBook(String author, String title) {
        this.author = author;
        this.title = title;
    }


    //======================================== setters && getters.
    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {

        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }


    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
    public int getFinalPrice() {
        return finalPrice;
    }

    public void setFinalPrice(int finalPrice) {
        this.finalPrice = finalPrice;
    }
}

共有1个答案

苏波涛
2023-03-14

改变

arrOfBBooks[counter].setAuthor(strsOfATI[0]);
arrOfBBooks[counter].setTitle(strsOfATI[1]);
arrOfBBooks[counter].setIsbn(strsOfATI[2]);

/*
BookstoreBook bb = new BookstoreBook();
bb.setAuthor(strsOfATI[0]);
bb.setTitle(strsOfATI[1]);
bb.setIsbn(strsOfATI[2]);
*/
// useing construct method is more elegant, my bad.
arrOfBBooks[counter] = new BookstoreBook(strsOfATI[0], strsOfATI[1], strsOfATI[2]);

而班级书店的书应该是静态的。

BookstoreBook arrOfBBooks[] = new BookstoreBook[100];

此语句仅创建一个数组对象,但数组的每个插槽仍然为null,因此无法通过调用方法 运算符

 类似资料:
  • 问题内容: 我遇到一种情况(在硒测试期间),在这种情况下,用户将收到安全代码。然后,用户必须先输入安全密码,然后才能继续操作。 我不太确定如何获得用户输入的值。我浏览了硒文档,并提出了这个建议。不幸的是,它并不是很有效。 有人可以指出我正确的方向吗? 问题答案: 似乎您必须先接受并关闭提示,然后才能存储和使用该值

  • 问题内容: 我有一种情况(在selenium测试期间),在这种情况下,用户将收到安全代码。然后,用户必须先输入安全代码,然后才能继续操作。 我不太确定如何获得用户输入的值。我浏览了selenium文档,并提出了这个建议。不幸的是,它并不是很有效。 有人可以指出我正确的方向吗? 问题答案: 似乎您必须先接受并关闭提示,然后才能存储和使用该值

  • 我正在为java类的介绍进行分配,并且遇到了困难,问题如下: “要求用户输入一个数字。您应该使用一个输入对话框来进行此输入。请确保将对话框中的字符串转换为实数。程序需要跟踪用户输入的最小数字和输入的最大数字。询问用户是否要输入另一个数字。如果是,请重复此过程。如果不是,请输出用户输入。 当用户想要退出时,此程序在程序末尾输出最大和最小的数字。 此外,您的程序应该考虑到用户只输入一个数字的情况。在这

  • 问题内容: 是否有某个功能全面的功能可以很好地用于清理用户针对SQL注入和XSS攻击的输入,同时仍然允许某些类型的HTML标签? 问题答案: 常见的误解是可以过滤用户输入。PHP甚至有一个(现在已弃用的)“功能”,称为magic-quotes,它基于此思想。废话 忘记过滤(或清洁,或任何人称呼它)。 为避免出现问题,您应该做的事情很简单:每当将字符串嵌入外部代码中时,都必须根据该语言的规则对其进行

  • 我在项目中有以下课程: 账户 客户 名称:fName和lName(都是字符串字段) 日期:年、月、日(均为整数字段) 银行:包含账户收款 InputReader:从键盘读取输入 账户对象需要客户对象和期初余额。客户对象需要名称对象和日期对象。Name对象需要名字和姓氏的字符串我需要向用户询问创建Name和Date对象的详细信息,以及期初余额。 我必须通过从用户那里获取相关信息来创建一个新帐户,即它

  • 问题内容: 嗨,我正在尝试创建一个程序来在用户为某个对象输入新信息时创建一个新对象。目前我有这个。 电脑超一流 桌面子类 笔记本电脑子类 因此,有一个班级和两个子班级,但我认为这并不重要。因此,如果我输入1,则应该为Desktop创建一个新对象,如果我输入2,则将为Laptop创建一个新对象。当我输入3时,它应该显示所有创建的对象。我不知道如何让他们工作,请帮助。 问题答案: 使用您提供的限制信息