0

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!

look at the run : enter image description here

Waiting for your help and thank you very much

c0der
  • 15,550
  • 6
  • 26
  • 53
  • 1
    i tried to understand the title of the question, but i can not get it. – The Scientific Method Oct 20 '18 at 15:15
  • Your question **is** an exact duplicate, you need to understand this to move forward. You are mixing line oriented input with non-line oriented input. The `input.nextLine();` needs to be after `input.nextDouble();` (because **that** leaves a trailing newline - 20.0 doesn't contain `\n`). **Or**, *alternatively* **always** use `input.nextLine()` and parse the `double`, `int`, or whatever yourself - that avoids the problem. – Elliott Frisch Oct 20 '18 at 15:17
  • `String email=input.next();` **and** `double price=input.nextDouble();` are enumerated ***examples*** in *is skipping `nextLine()` after using `next()` or `nextFoo()`* – Elliott Frisch Oct 20 '18 at 15:25
  • @ElliottFrisch Frisch I can't understand, if you can show me by code because I can't understand by theory instructions, i will be so thankful – Zahraa Maher Oct 20 '18 at 15:27

1 Answers1

0

Exactly as it says in this answer to Scanner is skipping nextLine() after using next() or nextFoo()?, you have to consume the trailing new line after every next() or nextFoo(). Like,

for (int i = 0; i < books.length; i++) {
    System.out.print("Enter book " + (i + 1) + " title: ");
    String title = input.nextLine();
    // NOT HERE. 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(); // next or nextFoo
    input.nextLine(); // Scanner is skipping nextLine

    System.out.println("Enter book " + (i + 1) + " author's email: ");
    String email = input.next(); // next or nextFoo
    input.nextLine(); // Scanner is skipping nextLine
    Author a = new Author(name, email);
    books[i] = new Book(title, price, a);
    System.out.println(books[i] + "\n");
}

or, as also suggested in that excellent answer, parse the input yourself and always use nextLine(). Like,

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

    System.out.print("Enter book " + (i + 1) + " price: ");
    String pr = input.nextLine();
    double price = Double.parseDouble(pr);

    System.out.println("Enter book " + (i + 1) + " author's email: ");
    String email = input.nextLine();
    Author a = new Author(name, email);
    books[i] = new Book(title, price, a);
    System.out.println(books[i] + "\n");
}
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226