文件输入 Java Mac

发布于 2024-10-21 11:59:00 字数 440 浏览 2 评论 0原文

我正在编写一个程序,该程序进入基本的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾听心声的旋律 2024-10-28 11:59:00

Java 的 File 类实际上并不处理为您打开或阅读。您可能需要查看 FileReaderBufferedReader 类。

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.

蓝天白云 2024-10-28 11:59:00

不用担心它是在 Mac 还是 Windows 上运行。 Java 会为您处理所有事务。 :)

你可以做类似下面的事情。这只是一个快速解决方案,因此您可能需要进行一些更改。它现在从名为“input.txt”的文件中读取。

import java.io.*;

public class ParseLines {
  public static void main(String[] args) {
    try {
      FileInputStream fs = new FileInputStream("input.txt");
      BufferedReader reader =
        new BufferedReader(new InputStreamReader(new DataInputStream(fs)));

      String line;
      double collect[] = {0.0, 0.0, 0.0, 0.0};
      int lines = 0;
      while ((line = reader.readLine()) != null) {
        String[] parts = line.split(",");
        if (parts.length == 5) {
          int avg = 0;
          boolean skip = false;
          for (int i = 1; i < 5; i++) {
            String part = parts[i].trim();

            try {
              int num = Integer.valueOf(part);
              avg += num;
              collect[i - 1] += (double) num;
            }
            catch (NumberFormatException nexp) {
              System.err.println("Input '" + part +
                                 "' not a number on line: " + line);
              skip = true;
              break;
            }
          }

          if (skip) continue;

          avg /= 4;
          lines++;                    

          System.out.println("Last name: " + parts[0].split(" ")[1] +
                           ", Avg.: " + avg);
        }
        else {
          System.err.println("Ignored line: " + line);
        }
      }

      for (int i = 0; i < 4; i++) {
        collect[i] /= (double) lines;
        System.out.println("Average of column " + (i + 1) + ": " +
                           collect[i]);
      }

      reader.close();
    } catch (Exception e){
      System.err.println("Error: " + e.getMessage());
    }    
  }
}

编辑:更改代码以平均第一、第二、第三和第四列。

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.

import java.io.*;

public class ParseLines {
  public static void main(String[] args) {
    try {
      FileInputStream fs = new FileInputStream("input.txt");
      BufferedReader reader =
        new BufferedReader(new InputStreamReader(new DataInputStream(fs)));

      String line;
      double collect[] = {0.0, 0.0, 0.0, 0.0};
      int lines = 0;
      while ((line = reader.readLine()) != null) {
        String[] parts = line.split(",");
        if (parts.length == 5) {
          int avg = 0;
          boolean skip = false;
          for (int i = 1; i < 5; i++) {
            String part = parts[i].trim();

            try {
              int num = Integer.valueOf(part);
              avg += num;
              collect[i - 1] += (double) num;
            }
            catch (NumberFormatException nexp) {
              System.err.println("Input '" + part +
                                 "' not a number on line: " + line);
              skip = true;
              break;
            }
          }

          if (skip) continue;

          avg /= 4;
          lines++;                    

          System.out.println("Last name: " + parts[0].split(" ")[1] +
                           ", Avg.: " + avg);
        }
        else {
          System.err.println("Ignored line: " + line);
        }
      }

      for (int i = 0; i < 4; i++) {
        collect[i] /= (double) lines;
        System.out.println("Average of column " + (i + 1) + ": " +
                           collect[i]);
      }

      reader.close();
    } catch (Exception e){
      System.err.println("Error: " + e.getMessage());
    }    
  }
}

Edit: Altered the code to average the first, second, third and fourth column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文