文件输入 Java Mac
我正在编写一个程序,该程序进入基本的 .txt 文件并打印某些内容。它是一个以逗号分隔的文件。该文件包含 7 个名字和姓氏,以及后面的 4 个数字。七个中的每一个都占一行。
每行看起来像这样:
乔治·华盛顿,7、15、20、14
该程序必须获取姓氏,然后对 4 个数字进行平均,还要对所有七个数字中的第一个、所有七个中的第二个进行平均,等等。我不知道如何开始接近这个并让它继续抓取和打印必要的内容。感谢您的任何帮助。我很感激。我使用 Mac 来完成所有这些编程,并且它需要在 Windows 上运行,所以我使用 :
File Grades = new File(System.getProperty("user.home"), "grades.txt");
那么我将如何使用它来读取文件呢?
I'm writing up a program that goes into a basic .txt file and prints certain things. It is a comma-delimited file. The file includes 7 first and last names, and also 4 numbers after. Each of the seven on a separate line.
Each line looks like this:
George Washington, 7, 15, 20, 14
The program has to grab the last name and then average the 4 numbers, but also average the first from all seven, second from all seven, etc. I'm not sure on how to start approaching this and get it to keep grabbing and printing what's necessary. Thanks for any help. I appreciate it. I'm using a Mac to do all of this programming and it needs to run on Windows so I'm using :
File Grades = new File(System.getProperty("user.home"), "grades.txt");
so how would I use that to read from the file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java 的 File 类实际上并不处理为您打开或阅读。您可能需要查看 FileReader 和BufferedReader 类。
Java's File class doesn't actually handle the opening or reading for you. You may want to look at the FileReader and BufferedReader classes.
不用担心它是在 Mac 还是 Windows 上运行。 Java 会为您处理所有事务。 :)
你可以做类似下面的事情。这只是一个快速解决方案,因此您可能需要进行一些更改。它现在从名为“input.txt”的文件中读取。
编辑:更改代码以平均第一、第二、第三和第四列。
Don't worry about whether it's running on Mac or Windows. Java takes care of all the business for you. :)
You could do something like the following. It's just a quick solution so you might want to do some changes perhaps. It reads from a file named "input.txt" right now.
Edit: Altered the code to average the first, second, third and fourth column.