Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My java for loop skips a step

I am writing an application for an exercise in a course I am doing at tech. It is supposed to instantiate 5 objects of the class Book, which contains data fields for a book's title, author and number of pages. I am having issues with a for.. loop. It skips a step every time after the first loop and I cannot figure out 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 for loop, it loops once, prints the book's title, author and number of pages I entered, like it should. But the second time, it prints "Please enter the books title" then skips straight to the second println without waiting for input. I am new to arrays of objects, and java in general, so any help is appreciated. Thanks in advance.

like image 909
Steven Ayerst Avatar asked Sep 18 '26 05:09

Steven Ayerst


2 Answers

Let me guess, you are typing something like "13<enter>" for the number of pages, right?

You are using the program wrong. Don't hit enter after the number of pages in the book. Immediately type the title of the next book with no spaces or anything. The code reads an integer and then reads a line for the title. So you cannot put anything between the integer and the title as that's not what the code expected.

This makes the program awfully hard to use though because the prompt to enter the title will appear after you have already entered the title. That's a pretty silly way to write a program, don't you think?

A simple fix: after kb.nextInt, call kb.nextLine and throw the empty line away.

like image 75
David Schwartz Avatar answered Sep 19 '26 18:09

David Schwartz


This line:

name = kb.nextLine();

is reading as many characters as it can until it finds a new line character, then reads in that character as well. Whereas this line:

pages = kb.nextInt();

is reading in a sequence of number characters, but leaving the new line character untouched.

The next time you go through the loop, there's a new line character hanging around which hasn't been read yet. So the kb.nextLine() dutifully reads that character (even though there are no characters before it) and continues on.

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

like image 40
Erica Avatar answered Sep 19 '26 18:09

Erica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!