Java 编译器将跳过字符串值数据成员

Java 编译器将跳过字符串值数据成员,java,Java,字符串值未显示,但已通知计算机显示请帮助 import java.util.Scanner; class Author { private String name; private String email; private char gender; public Author (String name, String email, char gender) { this.name=name; this.setEmail(ema

字符串值未显示,但已通知计算机显示请帮助

import java.util.Scanner;

class Author
{
    private String name;
    private String email;
    private char gender;

    public Author (String name, String email, char gender) {
        this.name=name;
        this.setEmail(email);
        this.gender=gender;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }
    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }
    /**
     * @return the gender
     */
    public char getGender() {
        return gender;
    }

    @Override
    public String toString() {
        return "Author [name=" + name + ", email=" + email + ", gender=" + gender + "]";
    }


}
class Book
{
    private String Name;
    Author auth;
    private double price;
    private int qty;

    /**
     * @param name
     * @param auth
     * @param price
     */
    public Book(String name, Author auth, double price) {
        super();
        Name = name;
        this.auth = auth;
        this.price = price;
    }

    /**
     * @param name
     * @param auth
     * @param price
     * @param qty
     */
    public Book(String name, Author auth, double price, int qty) {
        super();
        Name = name;
        this.auth = auth;
        this.price = price;
        this.qty = qty;
    }

    public String getName() {
        return Name;
    }


    public Author getAuth() {
        return auth;
    }



    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQty() {
        return qty;
    }

    public void setQty(int qty) {
        this.qty = qty;
    }

    @Override
    public String toString() {
        return "Book [Name=" + Name + ", auth=" + auth + ", price=" + price + ", qty=" + qty + "]";
    }



}
class Student
{
    private String Name;
    private int roll;
    date issueDate;
    date returnDate;
    /**
     * @param name
     * @param roll
     * @param issueDate``
     * @param returnDate
     */
    public Student(String name, int roll, date issueDate, date returnDate) {
        super();
        Name = name;
        this.roll = roll;
        this.issueDate = issueDate;
        this.returnDate = returnDate;
    }
    public String getName() {
        return Name;
    }

    public int getRoll() {
        return roll;
    }

    public date getIssueDate() {

        return issueDate;
    }

    public date getReturnDate() {
        return returnDate;
    }
    public void setReturnDate(date returnDate) {
        this.returnDate = returnDate;

    }



}
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        Author ahTeck=null;
        System.out.println("How many Book are there in library ?");
        int n=sc.nextInt();
        Book []ob=new Book[n];
        for(int i=0;i<n;++i)
        {   System.out.println("Author's name");
            String s=sc.nextLine();
            sc.nextLine();
            System.out.println("Author's Email Id");
            String s1=sc.nextLine();
            System.out.println("gender:");
            char c = sc.next(".").charAt(0);
            sc.nextLine();
            System.out.println("Book Name:");
            String b=sc.nextLine();
            System.out.println("Book price:");
            double price=sc.nextInt();
            System.out.println("Book quantity");
            int q=sc.nextInt();
            ob[i]=new Book(b, new Author(s, s1, c),price,q);
            System.out.println(ob[i]);
        }

    }

}
import java.util.Scanner;
类作者
{
私有字符串名称;
私人字符串电子邮件;
私人性别;
公共作者(字符串名称、字符串电子邮件、字符性别){
this.name=name;
这个.setEmail(email);
这个。性别=性别;
}
/**
*@返回名称
*/
公共字符串getName(){
返回名称;
}
/**
*@返回电子邮件
*/
公共字符串getEmail(){
回复邮件;
}
/**
*@param通过电子邮件发送要设置的电子邮件
*/
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
/**
*@返回性别
*/
公共字符getGender(){
返回性别;
}
@凌驾
公共字符串toString(){
返回“Author[name=“+name+”,email=“+email+”,gender=“+gender+””;
}
}
课堂用书
{
私有字符串名称;
作者授权;
私人双价;
私人整数数量;
/**
*@param name
*@param auth
*@param价格
*/
公共图书(字符串名称、作者身份、双倍价格){
超级();
名称=名称;
this.auth=auth;
这个价格=价格;
}
/**
*@param name
*@param auth
*@param价格
*@param数量
*/
公共图书(字符串名称、作者身份、双倍价格、整数数量){
超级();
名称=名称;
this.auth=auth;
这个价格=价格;
该数量=数量;
}
公共字符串getName(){
返回名称;
}
公共作者getAuth(){
返回auth;
}
公开双价{
退货价格;
}
公共定价(双倍价格){
这个价格=价格;
}
public int getQty(){
退货数量;
}
公共作废设置数量(整数数量){
该数量=数量;
}
@凌驾
公共字符串toString(){
返回“Book[Name=“+Name+”,auth=“+auth+”,price=“+price+”,qty=“+qty+””;
}
}
班级学生
{
私有字符串名称;
私人整数滚动;
发布日期;
返回日期;
/**
*@param name
*@param-roll
*@param-issueDate``
*@param returnDate
*/
公立学生(字符串名称、整数卷、发布日期、返回日期){
超级();
名称=名称;
this.roll=roll;
this.issueDate=issueDate;
this.returnDate=返回日期;
}
公共字符串getName(){
返回名称;
}
公共int getRoll(){
回程辊;
}
公开日期getIssueDate(){
发出的退货通知;
}
公共日期getReturnDate(){
返回日期;
}
公共作废setReturnDate(日期returnDate){
this.returnDate=返回日期;
}
}
公共班机{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
扫描仪sc=新的扫描仪(System.in);
作者ahTeck=null;
图书馆里有多少本书;
int n=sc.nextInt();
Book[]ob=新书[n];

对于(inti=0;i啊,是的,是的

在顶部输入整数后,立即调用sc.nextLine()

实际上,在输入字符串或字符后,您甚至不必调用
sc.nextLine()
。问题是,当您输入数字时,您也会按enter键。当您使用
nextInt()时,Scanner类将其视为另一个令牌
,因此号码被正确存储,但是回车键
\n
被视为另一个令牌。


因此,当您在输入号码后调用
nextLine()
时,系统会发现Scanner对象中已经有一个令牌,因此它将“\n”作为其输入。因此,名称实际上存储了
“\n”
,它是空的。

欢迎使用stackoverflow。请参考如何提出可回答的问题以及如何创建最少的可复制示例。您是否尝试调试代码?如果没有,为什么没有,如果有,为什么没有帮助?调试也尝试过,但也跳过了出现问题的部分。问题发生在aut中hor类ToString()名称不正确diplayed@SurajPanda您看到我的方法了吗?这是Scanner类的一个常见问题。@Robo Mop您正在谈论的是哪种方法,以及如何校正Scanner clsissue@SurajPanda我已经提交了答案。
int n = sc.nextInt();
sc.nextLine();