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

the file is:

The Lord Of the Rings
J.R.R. Tolkein
Great Expectations
Charles Dickens
Green Eggs and Ham
Dr. Seuss
Tom Sawyer
Mark Twain
Moby Dick
Herman Melville
The Three Musketeers
Alexander Dumas
The Hunger Games
Suzanne Collins
1984
George Orwell
Gone With the Wind
Margret Mitchell
Life of Pi
Yann Martel

It has the title of the book in first line and in the next line it has author.

I only want to read first five books and authors from the file and then store it in a stack. Add the first two books with author in a stack and then remove the last one. How can I do it?
This is what I did

Stack<Book> readingList = new Stack<>();



    File myFile = new File("books.txt");
    Scanner input = new Scanner(myFile);
    int i = 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());
    }

read more

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

the file is:

The Lord Of the Rings
J.R.R. Tolkein
Great Expectations
Charles Dickens
Green Eggs and Ham
Dr. Seuss
Tom Sawyer
Mark Twain
Moby Dick
Herman Melville
The Three Musketeers
Alexander Dumas
The Hunger Games
Suzanne Collins
1984
George Orwell
Gone With the Wind
Margret Mitchell
Life of Pi
Yann Martel

It has the title of the book in first line and in the next line it has author.

I only want to read first five books and authors from the file and then store it in a stack. Add the first two books with author in a stack and then remove the last one. How can I do it?
This is what I did

Stack<Book> readingList = new Stack<>();



    File myFile = new File("books.txt");
    Scanner input = new Scanner(myFile);
    int i = 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());
    }

read more

Aparapi GPU execution slower than CPU

I am trying to test the performance of Aparapi.
I have seen some blogs where the results show that Aparapi does improve the performance while doing data parallel operations.

But I am not able to see that in my tests. Here is what I did, I wrote two programs, one using Aparapi, the other one using normal loops.

Program 1: In Aparapi

import com.amd.aparapi.Kernel;
import com.amd.aparapi.Range;

public class App 
{
    public static void main( String[] args )
    {
        final int size = 50000000;

        final float[] a = new float[size];
        final float[] b = new float[size];

        for (int i = 0; i < size; i++) {
           a[i] = (float) (Math.random() * 100);
           b[i] = (float) (Math.random() * 100);
        }

        final float[] sum = new float[size];

        Kernel kernel = new Kernel(){
           @Override public void run() {
              int gid = getGlobalId();
              sum[gid] = a[gid] + b[gid];
           }
        };
        long t1 = System.currentTimeMillis();
        kernel.execute(Range.create(size));
        long t2 = System.currentTimeMillis();
        System.out.println("Execution mode = "+kernel.getExecutionMode());
        kernel.dispose();
        System.out.println(t2-t1);
    }
}

read more