开发者

File Input Java Mac

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 necessar开发者_如何学Pythony. 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜