从文本文件求和并打印它们

时间:2017-11-16 13:51:28

标签: java file sum

我正在尝试从文本文件中添加图书的成本并打印出来。但是,我得到[Ljava.lang.Double;@55f96302我的值应该是。

这是我目前的代码:

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");
    }
}

这是我的文本文件:

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

以下是我的java程序显示的内容:

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:

3 个答案:

答案 0 :(得分:1)

java.lang.Double@55f96302的当前输出是toString()包装类的Double输出,它继承了Object类的toString()方法。您看到的值是该对象的内存地址。

您希望输出值的String表示形式。为此,您可以使用doubleValue() Double方法将Object从对象转换为基本类型:double

在此处使用BookPrice2.doubleValue()

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

附注:Java中的变量名称应以小写字母开头,因此BookPrice2应命名为bookPrice2。在继续学习Java的过程中,您将学习更多的命名约定。

答案 1 :(得分:1)

变量BookPrice2似乎是一个数组,它在中保存了。在这种情况下,您不想直接在BookPrice2上调用方法,您希望迭代数组并将每个值添加到另一个变量。

这样的事情会让你走上正确的道路。

double sum = 0;
for (int i = 0; i < BookPrice2.length; i++) {
    sum += BookPrice2[i];
}
System.out.println(sum);

答案 2 :(得分:0)

最后整理了它

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

谢谢大家的帮助。现在达到平均,最低和最高价格:))