PoundingHead
PoundingHead

Reputation: 31

Global Scanner works once but gets a bit hairy on the second run

I know this site can be pretty harsh to noobs like myself but I have tried for three days to get this right. This is part of a homework assignment for a final project. The problem that I have is when I use case 1 more than once, the first time it works as planned but the second time it seems to skip over the following lines.

System.out.println("Please enter the title: ");
        String title = two.nextLine();

The areas that I am focusing on are in the Bookstore class in the add()method and in the main class, case 1. When trying to add a second book, the code immediately skips past the book title and goes straight to the ISBN.

import java.util.Scanner;
public class MIS_Tester {
public static Scanner two = new Scanner(System.in);
public static Scanner three = new Scanner(System.in);
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Bookstore bookseller = new Bookstore();
        Order addBook = new Order();
        Sale sell = new Sale();
        boolean finished = false;
        while(!finished){
            System.out.println("Select: (0) output; (1) add book; (2) delete book; (3) order book;"
                    + "(4) sell a book; (5) exit");
            int option = in.nextInt();
            switch(option){
            case 0: bookseller.ouput();break;
            case 1: bookseller.add(two);break;
            case 2: bookseller.delete(two);break;
            case 3: addBook.add(three);break;
            case 4: sell.sellBook(two);break;
            default: finished = true; break;
            }
        }   
        in.close();
    }
}

class Bookstore{
    private Book[] catalog;
    private int numBooks;
    private final int MAXBOOKNUM = 100;
    Scanner nine = new Scanner(System.in);

    public Bookstore(){
        numBooks = 0;
        catalog = new Book[MAXBOOKNUM];
    }
    public void ouput(){
        for (int i = 0; i < numBooks; i++) catalog[i].output();
    }
    public void add(Scanner two){
        System.out.println("Please enter the title: ");
        String title = two.nextLine();
        System.out.println("Enter the ISBN: ");
        int isbn = two.nextInt();
        System.out.println("Enter the publication date: ");
        int month = two.nextInt();int day = two.nextInt();int year = two.nextInt();
        catalog[numBooks++] = new Book(title, isbn, new Date(month, day, year));
    }
    public void delete(Scanner two){
        System.out.println("enter the one to delete");
        int option = two.nextInt();
        int numOfElements = catalog.length - (option +1);
        System.arraycopy(catalog, option + 1, catalog, option, numOfElements);
    }
}

class Book{
    private String title;
    private Date pubDate;
    private int isbn;
    private int orderNumber;

    public Book(String title, int isbn, Date pubDate){
        this.title = title;
        this.isbn = isbn;
        this.pubDate = pubDate;
    }

    public Book(String title, int isbn, Date pubDate, int orderNumber){
        this.title = title;
        this.isbn = isbn;
        this.pubDate = pubDate;
        this.orderNumber = orderNumber;
    }
    public void output(){
        System.out.println("Title: " + "\"" +title + "\"");
        System.out.println("ISBN: " + isbn);
        pubDate.output();
    }
    public void orderOut(){
        System.out.println("The book " + "\"" + title + "\"" + " has been ordered.");
    }
}

Console Log:

Select: (0) output; (1) add book; (2) delete book; (3) order book;(4) sell a book; (5) exit
1
Please enter the title: 
All About Dogs
Enter the ISBN: 
1234
Enter the publication date: 
12
29
1978
Select: (0) output; (1) add book; (2) delete book; (3) order book;(4) sell a book; (5) exit
1
Please enter the title: 
Enter the ISBN: 

Upvotes: 1

Views: 37

Answers (1)

sebenalern
sebenalern

Reputation: 2561

So in the java api located here.

nextLine() - Advances this scanner past the current line and returns the input that was skipped.

So this method advances the scanner to a new line and returns the input that was skipped.

nextInt() - Scans the next token of the input as an int.

This method only gets the next token of the input as an int. It does not consume the newline.

So basically what is happening is:

You are creating newlines when you use System.out.println but nextInt() method does not consume newline tokens so they are left in the input buffer. So when you call nextLine() the second time it only returns the newline token that is next in the input buffer(that was leftover). I hope this makes sense let me know if I should elaborate more.

Workaround: - Add a two.nextLine() //after each call to two.nextInt()

So your updated add() method would look like this:

public void add(Scanner two){
    System.out.println("Please enter the title: ");
    String title = two.nextLine();
    System.out.println("Enter the ISBN: ");
    int isbn = two.nextInt();
    two.nextLine(); // consume the left over newline token in the buffer
    System.out.println("Enter the publication date: ");
    int month = two.nextInt();int day = two.nextInt();int year = two.nextInt();
    two.nextLine(); // consume the left over newline token in the buffer
    catalog[numBooks++] = new Book(title, isbn, new Date(month, day, year));
}

Note this code was not tested.

Upvotes: 1

Related Questions