如何使用getter和setter获取用户输入

时间:2019-05-26 06:51:39

标签: java getter-setter

我已经创建了一个getter和setter类作为book,从中我也访问了类似get方法的setbookName,setbookPrice,setAuthorName,但是我无法接收用户的输入,请检查我的代码

myFunction()
  .then(() => // do something)
  .catch(() => // show my custom alert);

//图书课

Scanner sc=new Scanner(System.in);
try
{
    System.out.println("Enter book name:");
    String bookName=sc.nextLine();
    System.out.println("Enter book price:");
    String bookPrice=sc.nextLine();
    System.out.println("Enter author name:");
    String authorName=sc.nextLine();
}
catch(Exception e)
{
    System.out.println("I/O Exception");
}
book b=new book();
b.setBookName(bookName);          //here i am facing error
b.setBookPrice(bookPrice);
b.setAuthorName(authorName);
    System.out.println("Book Details");
    System.out.println("Book Name"+b.getBookName());
    System.out.println("Book Name"+b.getBookPrice());
    System.out.println("Book Name"+b.getAuthorName());
}

2 个答案:

答案 0 :(得分:0)

以下是您的主要代码-请查看我在其中添加的评论...

供以后参考,如果您包括了所收到的错误消息,将很有帮助。我假设代码代表您的工作:

Scanner sc=new Scanner(System.in);
try
{
    System.out.println("Enter book name:");
    String bookName=sc.nextLine();
    System.out.println("Enter book price:");
    String bookPrice=sc.nextLine();
    System.out.println("Enter author name:");
    String authorName=sc.nextLine();
}
/********************
 * The variables bookName, bookPrice, authorName cease to exist after the above
 * curly brace.
 * If you want to access them beyond this point, you must declare them before
 * the try { statement.
 * Alternatively, move the declaration of your Book prior to the try statement
 * and set the properties of the book in the above try block
 * e.g. use b.setBookName(sc.nextLine())
 * instead of reading it into the String bookName variable.
 *************************************************/
catch(Exception e)
{
    System.out.println("I/O Exception");
}
book b=new book();
b.setBookName(bookName);          //here i am facing error
b.setBookPrice(bookPrice);
b.setAuthorName(authorName);
    System.out.println("Book Details");
    System.out.println("Book Name"+b.getBookName());
    System.out.println("Book Name"+b.getBookPrice());
    System.out.println("Book Name"+b.getAuthorName());
}

以下是如何重写它的一个可能示例:

Scanner sc=new Scanner(System.in);
book b=new book();
try
{
    System.out.println("Enter book name:");
    b.setBookName(sc.nextLine());
    System.out.println("Enter book price:");
    b.setBookPrice(sc.nextLine());
    System.out.println("Enter author name:");
    b.setAuthorName(sc.nextLine());
}
catch(Exception e)
{
    System.out.println("I/O Exception");
}

    System.out.println("Book Details");
    System.out.println("Book Name"+b.getBookName());
    System.out.println("Book Name"+b.getBookPrice());
    System.out.println("Book Name"+b.getAuthorName());
}

答案 1 :(得分:0)

图书班

public class Book {
private String BookName;
private String AuthorName;
private double BookPrice;
public String getBookName() {
    return this.BookName;
}
public void setBookName(String bookName) {
    this.BookName = bookName;
}
public String getAuthorName() {
    return AuthorName;
}
public void setAuthorName(String authorName) {
    this.AuthorName = authorName;
}
public double getBookPrice() {
    return this.BookPrice;
}
public void setBookPrice(String bookPrice) {
    try {
        this.BookPrice =Double.valueOf(bookPrice);
    } catch (Exception e) {
        e.printStackTrace();
    }   
}}  

主班

public class App {
public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String bookName=null;//i think your error in hear
    String bookPrice=null;
    String authorName=null;
    try
    {
        System.out.println("Enter book name:");
        bookName=sc.nextLine();
        System.out.println("Enter book price:");
        bookPrice=sc.nextLine();
        System.out.println("Enter author name:");
        authorName=sc.nextLine();
    }
    catch(Exception e)
    {
        System.out.println("I/O Exception");
    }       
    Book b=new Book();
    b.setBookName(bookName);       
    b.setBookPrice(bookPrice);
    b.setAuthorName(authorName);
    System.out.println("Book Details");
    System.out.println("Book Name"+b.getBookName());
    System.out.println("Book Name"+b.getBookPrice());
    System.out.println("Book Name"+b.getAuthorName());      
    sc.close();
}}

这对我有用