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


i know site can pretty harsh noobs myself have tried 3 days right. part of homework assignment final project. problem have when use case 1 more once, first time works planned second time seems skip on following lines.

system.out.println("please enter title: ");         string title = two.nextline(); 

the areas focusing on in bookstore class in add()method , in main class, case 1. when trying add second book, code skips past book title , goes straight isbn.

import java.util.scanner; public class mis_tester { public static scanner 2 = new scanner(system.in); public static scanner 3 = 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 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 9 = new scanner(system.in);      public bookstore(){         numbooks = 0;         catalog = new book[maxbooknum];     }     public void ouput(){         (int = 0; < numbooks; i++) catalog[i].output();     }     public void add(scanner two){         system.out.println("please enter title: ");         string title = two.nextline();         system.out.println("enter isbn: ");         int isbn = two.nextint();         system.out.println("enter 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 1 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 book; (5) exit 1 please enter title:  dogs enter isbn:  1234 enter publication date:  12 29 1978 select: (0) output; (1) add book; (2) delete book; (3) order book;(4) sell book; (5) exit 1 please enter title:  enter isbn:  

so in java api located here.

nextline() - advances scanner past current line , returns input skipped.

so method advances scanner new line , returns input skipped.

nextint() - scans next token of input int.

this method gets next token of input int. not consume newline.

so happening is:

you creating newlines when use system.out.println nextint() method not consume newline tokens left in input buffer. when call nextline() second time returns newline token next in input buffer(that leftover). hope makes sense let me know if should elaborate more.

workaround: - add two.nextline() //after each call two.nextint()

so updated add() method this:

public void add(scanner two){     system.out.println("please enter title: ");     string title = two.nextline();     system.out.println("enter isbn: ");     int isbn = two.nextint();     two.nextline(); // consume left on newline token in buffer     system.out.println("enter publication date: ");     int month = two.nextint();int day = two.nextint();int year = two.nextint();     two.nextline(); // consume left on newline token in buffer     catalog[numbooks++] = new book(title, isbn, new date(month, day, year)); } 

note code not tested.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -