1

good afternoon

I am trying to make a java Project for a Book, that it has 2 calsses + main class

1- the first class is the Author:

package booktest;

public class Author {

private String name;
private String email;

public Author(){

}

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

public void setName(String name){
    this.name=name;
}

public String getName(){
    return name;
}

public void setEmail(){
    this.email=email;
}

public String getEmail(){
    return email;
}

public String toString(){
    return String.format("Author Information:\n name: %s\n email: %s", getName(), getEmail());
}

}

2- second class is Book:

package booktest;

public class Book {

    private String title;
    private double price;
    private Author bookAuthor;

    public Book(){

    }

    public Book(String title, double price, Author bookAuthor){
       this.title=title;
       this.price=price;
       this.bookAuthor=bookAuthor;

    }

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

    public String getTitle(){
        return title;
    }

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

    public double getPrice(){
        return price;
    }

    public void setAuthor(){
        this.bookAuthor=bookAuthor;
    }

    public Author getAuthor(){
        return bookAuthor;
    }

    public String toString(){
        return String.format("Book Information: \n title: %s\n price: %.2f SAR\n %s", getTitle(), getPrice(),getAuthor());
    }



}

3- the third class is the driver(main) class which called BookTest:

package booktest;

import java.util.Scanner;
public class BookTest {

    public static void main(String[] args) {
       Scanner input= new Scanner(System.in);

       // 1. making array of books (Array of objects):
       Book[] books= new Book[3];

       //2. stor the values into an array of objects:

        for (int i = 0; i <books.length; i++) {
            System.out.print("Enter book "+ (i+1)+ " title: ");
            String title=input.nextLine();
            System.out.println("Enter book "+(i+1)+" author's name:");
            String name=input.nextLine();

            System.out.print("Enter book "+ (i+1)+ " price: ");
            double price=input.nextDouble();


            System.out.println("Enter book "+(i+1)+" author's email: ");
            String email=input.next();

            Author a=new Author(name,email);

            books[i]=new Book(title,price,a);

            System.out.println(books[i]+"\n");



        }

    }

}

the proplem in the main class that when the loop first run (i=0 ), it let me fill the name of author and the book title, but when loop go to it's second run (i=1) it does nor distingush between the title (String title) and the book author name ( String name) and let me enter only 1 of them !

see the run here :

enter image description here


waiting for your help and thank you very much!

  • it does not solve the problem :( – Zahraa Maher Oct 20 '18 at 13:50
  • @JB Nizet the solution you put in another question does not work with my problem, it tried and nothing changed! – Zahraa Maher Oct 20 '18 at 13:53
  • 1
    Then ask another question showing what you tried, explaining that you understand that calling next() does not consume the EOL, but that even though you add a nextLine( after callling it, you still have a bug. – JB Nizet Oct 20 '18 at 14:04
  • @ZahraaMaher Did you read duplicate question? How did you try to correct your code to eliminate problem explained there? – Pshemo Oct 20 '18 at 14:12
  • @Pshemo Yes I did, I tried to insert an input.nextLine() between the String title=input.nextLine(); and System.out.println("Enter book "+(i+1)+" author's name:"); but nothing happens, it let me enter the both values, but when the object is printed , the title is missed! – Zahraa Maher Oct 20 '18 at 14:39
  • What do you think placing `input.nextLine()` *after* `String title=input.nextLine();` should achieve? It is not `nextLine()` which leaves line separator the problem. It is other `nextTYPENAME` methods which leaves it causing `nextLine()` to return empty line so you need to place it after last of those methods. – Pshemo Oct 20 '18 at 14:42
  • then what nextTYPENAME() should I use? – Zahraa Maher Oct 20 '18 at 14:53
  • It is not about which `nextTYPENAME` to use, it is about using additional `nextLine()` *after* it to consume line separators left while providing data by user. Please reread duplicate question (I also posted [answer](https://stackoverflow.com/a/39949330) there where I tried to explain this problem best way I can). – Pshemo Oct 20 '18 at 18:44

0 Answers0