reading only some strings from a file and storing it in stack in java -


the file is:

the lord of rings j.r.r. tolkein great expectations charles dickens green eggs , ham dr. seuss tom sawyer mark twain moby dick herman melville 3 musketeers alexander dumas hunger games suzanne collins 1984 george orwell gone wind margret mitchell life of pi yann martel 

it has title of book in first line , in next line has author.

i want read first 5 books , authors file , store in stack. add first 2 books author in stack , remove last one. how can it? did

stack<book> readinglist = new stack<>();        file myfile = new file("books.txt");     scanner input = new scanner(myfile);     int = 0;     while (input.hasnext()) {         readinglist.push(new book(input.nextline(), input.nextline()));       system.out.println("adding: " + readinglist.lastelement().getinfo());         readinglist.push(new book(input.nextline(), input.nextline()));       system.out.println("adding: " + readinglist.lastelement().getinfo());         system.out.println("reading:  " + readinglist.pop().getinfo());     } 

asumming every book + author in 1 line of file:

to read first 5 books use for-loop instead of while:

     stack<book> readinglist = new stack<>();                                file myfile = new file("books.txt");                             scanner input = new scanner(myfile);                             int = 0;                             int counter = 0;                             (int numberofbooktoread = 0; numberofbooktoread < 2;numberofbooktoread++) {                                 try {                                     if(readinglist.hasnextline() && counter <= 4){ // provides 4 books pushed while iteration going, , works                                         readinglist.push(new book(input.nextline(), input.nextline()));                                        counter += 1;                                        system.out.println("adding: " + readinglist.lastelement().getinfo());                                        readinglist.push(new book(input.nextline(), input.nextline()));                                        counter +=1;                                        system.out.println("adding: " + readinglist.lastelement().getinfo());                                        system.out.println("reading:  " + readinglist.pop().getinfo());                                        readinglist.pop() = null;                                     }catch(exception e){                                           e.printstacktrace();                                      }                                 } else if(readinglist.hasnextline){                                           readinglist.push(new book(input.nextline(), input.nextline()));                                           system.out.println("adding: " + readinglist.lastelement().getinfo());                                         }                             } 

afterwards clear last item stack:

    readinglist.pop() = null; 

because got lot of time. here same function variable number of maximum books:

stack<book> readinglist = new stack<>();                                file myfile = new file("books.txt");                             scanner input = new scanner(myfile);                             int = 0;                             int counter = 0;                             int maxnumberofbooks = 10; //could number wish                             (int numberofbooktoread = 0; numberofbooktoread < math.round(maxnumberofbooks/2)-1;numberofbooktoread++) {                                 try {                                     if(readinglist.hasnextline() && counter <= maxnumberofbooks-1){                                        readinglist.push(new book(input.nextline(), input.nextline()));                                        counter += 1;                                        system.out.println("adding: " + readinglist.lastelement().getinfo());                                        readinglist.push(new book(input.nextline(), input.nextline()));                                        counter +=1;                                        system.out.println("adding: " + readinglist.lastelement().getinfo());                                        system.out.println("reading:  " + readinglist.pop().getinfo());                                        readinglist.pop() = null;                                     }catch(exception e){                                           e.printstacktrace();                                      }                                 } else if(readinglist.hasnextline){                                           readinglist.push(new book(input.nextline(), input.nextline()));                                           system.out.println("adding: " + readinglist.lastelement().getinfo());                                         }                             } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -