0

I have set a constructor for a Class Book with author; title; price and page. Everything works okey. When I println the manual input and result on the first book, but when I try to write a manual input for the second book the println asks for title directly.

I red the code over and over again and it seems in order, same as for Book1. I just started studying JAVA at school and we need to research for most of the exercises so this one includes actually importing and using java.util.Scanner, while having two argument constructor. I searched to find a similar issue with this scanner, but apparently it's a sintax error that messes up a bit if you call for the constructor to reply twice or thrice.

**Here are some examples of my code and result:**
\\some code above ofcourse
    Scanner keyboard = new Scanner(System.in);

        //BOOK 1 manual entry.
        System.out.println("Your author1: ");
        String author1 = keyboard.nextLine();

        System.out.println("Title of your book1: " );
        String title1 = keyboard.nextLine();

        System.out.println("Price of your book1: ");
        double price1 = keyboard.nextDouble();

        System.out.println("Pages of your book1: ");
        int page1 = keyboard.nextInt();

        classbook book1 = new classbook(author1, title1, price1, page1);


        System.out.println("--------------------------------");
        book1.discount(0.20);
        System.out.println(book1);System.out.println("+ 10% added discount on your book.");

        System.out.println("--------------------------------");

    System.out.println("Your author2: ");
        String author2 = keyboard.nextLine();

        System.out.println("Title of your book2: " );
        String title2 = keyboard.nextLine();

        System.out.println("Price of your book2: ");
        double price2 = keyboard.nextDouble();

        System.out.println("Pages of your book2: ");
        int page2 = keyboard.nextInt();    



        classbook book2 = new classbook(author2, title2, price2, page2);

        System.out.println("--------------------------------");
        book2.discount(0.20);
        System.out.println(book2);System.out.println("+ 10% added discount on your book2.");

        System.out.println("--------------------------------");
        }
}

And here is a picture of the result: "Pic1: It asks me to input directly into second line (Title)"

enter image description here

"Pic2: The result shows that Author field is blank, the dot is directly recalled from the constructor just as an end to a text.

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
Seneca
  • 1
  • 1
  • Please look at [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Hovercraft Full Of Eels Feb 15 '17 at 14:17
  • As an aside: In the future your images should be posted as code-formatted text, not as images. Only use images for actual images. Thanks for your cooperation in this. – Hovercraft Full Of Eels Feb 15 '17 at 14:17

0 Answers0