如何使用循环从.txt中读取3个数据,创建对象并显示它们。 [英] How to use a loop to read in the 3 pieces of data from a .txt, create objects, and display them.?

查看:56
本文介绍了如何使用循环从.txt中读取3个数据,创建对象并显示它们。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经意识到我会使用资源类中的toString来显示它们,但是我对它的工作原理感到困惑。别误会我的意思;我知道如何制作java读取文本/ dat文件,但我不确定如何在读取文件后循环它们,并根据文件创建对象。另外,我如何让扫描仪读取字符串和数字?



这是文本文件中的内容:



cheese 12 12.50

香肠15 21.89

辣椒10 15.05

菠萝40 33.00

意大利辣香肠11 10.22

橄榄9 8.99
xcheese 13 9.66

至尊13 15.22

蘑菇17 16.34

bbqchicken 14 20.13

pepperoni 10 11.70

香肠12 11.89

辣椒12 15.05

菠萝14 13.50

squirrel 12 12.24

pickle 12 7.99

夏威夷10 13.00

意大利辣香肠11 10.22

肉13 9.66

sloppyJoe 15 15.30

甜点14 17.60

bigBubba 16 25.00

steakLovers 23 30.77

bison 10 11.70

secretMeat 12 11.99

pepper 12 14.00

pineNeedle 13 13.50

sweetTart 12 12.24

豆腐7 8.99

I already realize that I would display them using a toString from my resource class, but I'm perplexed on how that would work. Don't get me wrong; I know how to make java read text/dat files, but I'm not to sure how to loop them after reading the file, and creating objects based off the file. Also how would I get the scanner to read a string and a number?

Here's what's in the text file:

cheese 12 12.50
sausage 15 21.89
peppers 10 15.05
pineapple 40 33.00
pepperoni 11 10.22
olive 9 8.99
xcheese 13 9.66
supreme 13 15.22
mushroom 17 16.34
bbqchicken 14 20.13
pepperoni 10 11.70
sausage 12 11.89
peppers 12 15.05
pineapple 14 13.50
squirrel 12 12.24
pickle 12 7.99
hawaiian 10 13.00
pepperoni 11 10.22
meat 13 9.66
sloppyJoe 15 15.30
dessert 14 17.60
bigBubba 16 25.00
steakLovers 23 30.77
bison 10 11.70
secretMeat 12 11.99
peppers 12 14.00
pineNeedle 13 13.50
sweetTart 12 12.24
tofu 7 8.99

推荐答案

有很多资源可以在网上推荐。例如

逐行读取文本文件并在数组中存储对象 [ ^ ]

读取文件行逐行存储每个行的数组中的对象 [ ^ ]

用Java阅读文本文件 [ ^ ]

尝试基于这些实现并且如果您有问题请询问您的代码和问题详细信息(如果有错误详细信息)



there are plenty of resources available to refer in the internet. for example
Reading a text file line by line and storing an object in the array[^]
Reading a file line by line while storing an object in an array for each line[^]
Reading a text file in Java[^]
try to implement based on those and if you have issue ask question with your code and the problem details ( with error details if any)

引用:

我知道如何制作java读取文本/ dat文件,但是我我不确定如何在读取文件后循环它们,并根据文件创建对象。另外,我如何让扫描仪读取字符串和数字?

I know how to make java read text/dat files, but I'm not to sure how to loop them after reading the file, and creating objects based off the file. Also how would I get the scanner to read a string and a number?



是的,请参阅链接,您可以看到如何使用扫描仪读取字符串和数字。


Yes, refer the links and you can see how to use Scanner to read strings and numbers.


这篇关于如何使用循环从.txt中读取3个数据,创建对象并显示它们。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆