简体   繁体   English

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

[英]How to take user input using getter and setter

I have created a getter and setter class as book and from that i am accessing setbookName,setbookPrice,setAuthorName similar get methods too but i am not able to take inputs from user please check my code 我已经创建了一个getter和setter类作为书,从我访问setbookName,setbookPrice,setAuthorName类似get方法,但我无法从用户获取输入请检查我的代码

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());
}

// book class //书类

    class book 
   {

     private String bookName;
    private String authorName;
    private String bookPrice;

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    public String getBookPrice() {
        return bookPrice;
    }

    public void setBookPrice(String bookPrice) {
        this.bookPrice = bookPrice;
    }

Following is your main code - please see my comment that I've added to it... 以下是您的主要代码 - 请参阅我添加到其中的评论...

For future reference, it would be helpful if you included the error messages you are getting. 为了将来参考,如果您包含了您收到的错误消息,将会很有帮助。 I'm assuming that the code is representative of what you are doing: 我假设代码代表你在做什么:

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());
}

Following is one possible example of how to rewrite it: 以下是如何重写它的一个可能示例:

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());
}

Book Class 书类

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();
    }   
}}  

Main class 主要课程

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();
}}

this work for me 这项工作对我来说

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM