gpt4 book ai didi

java - 保存到文件,无法读取双重内容

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:30 24 4
gpt4 key购买 nike

我有这段代码将我的 Collection 保存到文件中,其格式如前所示。

{   
try {
PrintWriter save = new PrintWriter(path);
//writing collection to file
for(film f: films)
{
save.printf("%s%n",f.title);

save.printf("%d %d %f%n", f.marks, f.numberofmarks, f.average);}

save.close();
return true;
}
catch (FileNotFoundException e) {return false;}

}

保存的文件结构如下所示:

pokemon

20 7 2.857143

我使用以下代码从文件中读取

{
//opening scaner and taking care of ioexceptios

FileReader fr= null;
Scanner sc = null;

try {fr = new FileReader(path);}
catch (FileNotFoundException e) {return false;}

sc = new Scanner(fr);

while(sc.hasNext()){
film tmp = new film(); //creating new object
tmp.title = sc.nextLine(); //reading whole line and adding title
tmp.marks = sc.nextLong(); //2nd line "marks" are read from and added to object
tmp.numberofmarks = sc.nextLong(); // 2nd value is number of marks ,read and added to object
tmp.average = sc.nextDouble(); // last value (3rd) is average,read and added to object

films.add(tmp); //adding the object with read fields to database
sc.nextLine(); //moving the scanner to next line

}

sc.close(); //closing scanner
return true; //returning true is everything happened according to plan
}

每次当值为 Double 时,我都会在 nextdouble() 上收到输入不匹配的错误

最佳答案

几乎可以肯定区域设置。将您的点转换为逗号,或将以下内容放入您的代码中,然后重试。

scan.useLocale(Locale.US);

关于java - 保存到文件,无法读取双重内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28142559/

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