My java for loop skips step

I am writing an application for an exercise in the course that I do in tech. It is supposed to create 5 objects of a class Bookthat contains data fields for the name of the book, author and number of pages. I have problems with for.. loop. It skips a step every time after the first cycle, and I cannot understand why. Here is my code

import java.util.*;
public class LibraryBook2
{
    public static void main(String[]args)
{
    String name;
    String author;
    int pages;
    Book[] novel = new Book[5];
    novel[0] = new Book();
    novel[1] = new Book();
    novel[2] = new Book();
    novel[3] = new Book();
    novel[4] = new Book();
    Scanner kb = new Scanner(System.in);
    for (int i = 0; i< novel.length;)
    {   
        System.out.println("Please Enter the books title");
        name = kb.nextLine();
        novel[i].setTitle(name);
        System.out.println("Please enter the books author");
        author = kb.nextLine();
        novel[i].setAuthor(author);
        System.out.println("Please enter the number of pages in this book");
        pages = kb.nextInt();
        novel[i].setPages(pages);
        System.out.println(""+novel[i].title);
        System.out.println(""+novel[i].author);
        System.out.println(""+novel[i].pages);
        ++i;
    }
    for (int x = 0; x<novel.length; x++)
    {
    System.out.print(""+ novel[x].title + "\n" + novel[x].author + "\n" + novel[x].pages);
    }
  }
}

During the first cycle, forhe cyclically once, prints the name of the book, the author and the number of pages entered by me, as it should. But the second time he prints "Please enter the name of the book", then skips right to the second println, without waiting for input. I am new to object arrays and java in general, so any help is appreciated. Thanks in advance.

+3
3

:

public static void main(String []arg){
    String name;
    String author;
    String pages;
    Book[] novel = new Book[2];
    novel[0] = new Book();
    novel[1] = new Book();
    novel[2] = new Book();
    novel[3] = new Book();
    novel[4] = new Book();
    Scanner kb = new Scanner(System.in);
    for (int i = 0; i< novel.length;)
    {   
        System.out.println("Please Enter the books title");
        name = kb.nextLine();
        novel[i].setTitle(name);
        System.out.println("Please enter the books author");
        author = kb.nextLine();
        novel[i].setAuthor(author);
        System.out.println("Please enter the number of pages in this book");
        pages = kb.nextLine();
        novel[i].setPages(Integer.parseInt(pages));
        System.out.println(""+novel[i].title);
        System.out.println(""+novel[i].author);
        System.out.println(""+novel[i].getPages());
        ++i;
    }
    for (int x = 0; x<novel.length; x++)
    {
    System.out.print(""+ novel[x].title + "\n" + novel[x].author + "\n" + novel[x].pages);
    }

nextLine, .

0

, - "13 <enter> " , ?

. enter . - . , . , - , , .

, , . , ?

: kb.nextInt kb.nextLine .

+2

:

name = kb.nextLine();

reads as many characters as it can until it finds a new line symbol, and then reads that symbol. If this line:

pages = kb.nextInt();

read in a sequence of numeric characters, but leaving the new character of the string intact.

The next time you go through the loop, there will appear a new line symbol that has not yet been read. Thus, kb.nextLine () dutifully reads this character (even if there are no characters before it) and continues.

What you probably want to do is make sure that this extra newline is gone from the input buffer before the next loop.

+1
source

All Articles