gpt4 book ai didi

java - 对文本文件中的值求和并打印它们

转载 作者:行者123 更新时间:2023-12-02 02:29:45 25 4
gpt4 key购买 nike

我正在尝试从文本文件添加书籍的成本并打印它们。但是,我得到 [Ljava.lang.Double;@55f96302我的值(value)观应该在哪里。

这是我当前的代码:

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:

最佳答案

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

并且您想输出该值的字符串表示形式。为此,您可以使用 DoubledoubleValue() 方法将对象转换为原始类型:double

在此处使用BookPrice2.doubleValue():

System.out.println("书籍总成本:"+ BookPrice2.doubleValue() );//+ gb.format());

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

关于java - 对文本文件中的值求和并打印它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47331425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com