This problem related to my previous problem here:
Java does not differ between 2 String variables in array of objects for loop proplem
@JB Nizet has given me this link and marked my problem as duplicated: Scanner is skipping nextLine() after using next() or nextFoo()?
but I tried to do the what they said and it does not work well, you can see what I did as they said:
3- Main Class:
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. store 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();
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");
}
}
}
as you can see I added an input.nextLine(); line 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!
Waiting for your help and thank you very much