0

I'm trying to add the cost of books from a text file and print them. However, I'm getting [Ljava.lang.Double;@55f96302 where my values should be.

Here is my current code:

System.out.println("Welcome to the libary book search system.");
System.out.println("------------------------------------------");

if(choice.equals("Text File")) {

    //String sc = null; 
    System.out.println("Title\t\t\tAuthor\t\t\tPrice\t\t\tBookPublisher\t\t\tISBN");
    System.out.println("=====\t\t\t======\t\t\t=====\t\t\t=============\t\t\t====");

    int count = 0;

    try {
        File Fileobject = new File ("E:\\text files for java\\BooksToImport.txt");
        Scanner sc = new Scanner(Fileobject);

        // Read and display each line of the file
        while(sc.hasNextLine()) {
            String line2 = sc.nextLine();
            Scanner sc2 = new Scanner(line2);
            sc2.useDelimiter("-");
            System.out.println(line2);

            while(sc2.hasNext()) {
                BookTitle[count] = sc2.next();
                System.out.println(BookTitle[count]);
                BookAuthor[count] = (sc2.next());
                System.out.println(BookAuthor[count]);
                BookPrice[count] = sc2.next();
                System.out.println(BookPrice[count]);
                BookPublisher[count] = sc2.next();
                System.out.println(BookPublisher[count]);
                ISBN[count] = sc2.next();
                System.out.println(ISBN[count]);
            }

            sc2.close();
            count++;

        }       

        //for(int index=0;index<count;++index) {
            //BookPrice2[index]=Double.parseDouble(BookPrice[index]);
            //System.out.println(BookPrice2[index]);
        //}

        sc.close();         
        System.out.println("----------------------");   
        System.out.println("Totals of the Text File:");
        System.out.println("----------------------");
        System.out.println("Total Numbers Of Books:" + NUMBEROFBOOKS);
        System.out.println("Total Cost Of Books:"+ BookPrice2 );
        System.out.println("Maximum Cost Of A Book:"); 
        System.out.println("Minimum Cost Of A Book:");
        System.out.println("Average Cost Of A Book:"); 
        System.out.println("Total number of valid books:"); 
        System.out.println("Total number of invalid books:"); 
    } catch (FileNotFoundException e) {
            System.out.println("File does not exist");
    }
}

Here is my text file:

The Hunger Games - Suzanne Collins - 5 - Scholastic Press - 9781921988752
OOP programming - Graham Winter - 32 - Oriely - 9876543219
Harry potter - Jk Rowling - 10 - Bloomsbury - 9788700631625

Here is what is being displayed by my java program:

Enter what you want to do: Type 'Input' for user input or 'Text File' to read from file.

Text File

Welcome to the libary book search system.
------------------------------------------
Title           Author          Price           BookPublisher               ISBN
=====           ======          =====           =============           ====
The Hunger Games - Suzanne Collins - 5 - Scholastic Press - 9781921988752
The Hunger Games 
Suzanne Collins 
5 
Scholastic Press 
9781921988752
OOP programming - Graham Winter - 32 - Oriely - 9876543219
OOP programming 
Graham Winter 
32 
Oriely 
9876543219
Harry potter - Jk Rowling - 10 - Bloomsbury - 9788700631625
Harry potter 
Jk Rowling 
10 
Bloomsbury 
9788700631625
----------------------
Totals of the Text File:
----------------------
Total Numbers Of Books:3
Total Cost Of Books:[Ljava.lang.Double;@55f96302
Maximum Cost Of A Book:
Minimum Cost Of A Book:
Average Cost Of A Book:
Total number of valid books:
Total number of invalid books:
jordiburgos
  • 5,964
  • 4
  • 46
  • 80

3 Answers3

1

Your current output of java.lang.Double@55f96302 is the toString() output for the Double wrapper class which inherits the Object class's toString() method. The value you are seeing is the memory address for that object.

And you want to output a String representation of the value instead. To do this you would use the doubleValue() method of Double to convert from an Object to the primitive type: double.

Use BookPrice2.doubleValue() here:

System.out.println("Total Cost Of Books:"+ BookPrice2.doubleValue() ); //+ gb.format());

Side note: variable names in Java should begin with a lowercase letter so BookPrice2 should be named bookPrice2. You'll learn more naming conventions along the way as you continue to learn Java.

George Andrews
  • 279
  • 3
  • 9
  • Thank you for your help. However, when I use BookPrice2.doubleValue() in my print, I get the error: Cannot invoke doubleValue() on the array type Double[] do you know why? – Matty Harris Nov 16 '17 at 14:19
  • Some of your code must not be in the question. Looking at the code in the question, you would try something like this ```BookPrice[count].doubleValue()``` to print out the value in your while loop. BookPrice2 appears to be an array which is why your error references: ```Double[]```. You have more work to do to get the total cost of the books. You will need to create a variable of type ```Double``` or ```double``` to accumulate the total. – George Andrews Nov 16 '17 at 14:21
  • For example, after declaring a variable outside of the while loop e.g. ```Double totalPrice = 0.0;```, you would accumulate the value inside the loop ```totalPrice = totalPrice + Double.parseDouble(BookPrice[count].trim());```, and finally print the result: ```System.out.println("Total Cost Of Books:"+ totalPrice );``` Hope this helps. – George Andrews Nov 16 '17 at 14:30
1

It looks like the variable BookPrice2 is an array that holds doubles inside of it. In this case, you don't want to call a method directly on BookPrice2, you want to iterate over the array and add each value to another variable.

Something like this should set you on the correct path.

double sum = 0;
for (int i = 0; i < BookPrice2.length; i++) {
    sum += BookPrice2[i];
}
System.out.println(sum);
Scrambo
  • 579
  • 5
  • 17
  • I would also like to point out that I erred in my initial editing of OP's question. I errantly removed some commented out code that gave me the idea that @Matty was using the BookPrice2 as an array and not a Double object. I have put another edit in place to correct my mistake hopefully. – Scrambo Nov 16 '17 at 15:00
  • Thank you for your reply, however when using your code sustituted with my values my program does not want to read my further prints in my program eh 'Total Number of books' etc and comes up with the error 'Exception in thread "main" java.lang.NullPointerException' any idea why? – Matty Harris Nov 16 '17 at 15:06
  • Yes, a NullPointerException is what happens when you try to reference (by printing, making a method call, etc) an object that doesn't exist. Without seeing the code (or the whole error message) I would only be able to guess as to what actually is happening, so let me offer some help in a different way. [This](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) explains what a NullPointer is AND how to fix it. I'd suggest reading up there and then trying to figure out how that relates to your problem. – Scrambo Nov 16 '17 at 15:40
0

Finally sorted it

for (int x = 0; x < BookPrice.length; x++) {
                            total2 = total2 + Double.parseDouble(BookPrice[x]);
                        }
                        System.out.println(total2);

Thank you all for your help. Now onto the average, minimum and maximum prices :)