Home » Java » java – The string value data member is skipped by compiler-Exceptionshub

java – The string value data member is skipped by compiler-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

string value is not displaying all though told the computer to display Please help

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

    }

}

the question is there in
https://www.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html

Output is coming like

Book [Name=g, auth=Author [name=, email=e, gender=m], price=6.0, qty=6]

the displaying of author name is skipped

How to&Answers:

Ah yes. The ol’ Scanneroo.

Right after entering the integer at the top, call sc.nextLine();

int n = sc.nextInt();
sc.nextLine();

Actually, you don’t even have to call sc.nextLine() after entering a string or character. The thing is, when you enter a number, you hit the enter key as well. The Scanner class regards it as another token when you use nextInt(), so the number gets stored correctly, but the enter key \n is regarded as another token.

So when you call nextLine() after entering the number, the system sees that there is already a token remaining in the Scanner object, so it takes ‘\n’ as its input. Thus, the name actually stores "\n", which is empty.