-3

我需要帮助来阅读两个包含 2010 年和 2000 年人口普查的文件。我必须阅读这两个文件,然后找出这两个文件之间的人口增长。我一直在为单个状态获取 null。我知道我对 inLine1 和 inLine2 有空值。

文件看起来像这样

Alabama,4779736
Alaska,710231
Arizona,6392017
Arkansas,2915918

代码:

    import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class pa10
{
  public static void main(String[] args, char[] inLine2, char[] inLine1)
    throws java.io.IOException
  {
    String fileName1 = "Census2000growth.txt";
    String fileName2 = "Census2010growth.txt";
    int i;

    File f = new File("Census2010growth.txt");
    if(!f.exists()) {
        System.out.println( "file does not exist ");
    }
    Scanner infile = new Scanner(f);
    infile.useDelimiter ("[\t|,|\n|\r]+"); //create a delimiter 
    final int MAX = 51;
    int [] myarray = new int [MAX];
    String[] statearray = new String[MAX];
    int fillsize;

      // set up input stream1
    FileReader fr1 = new
        FileReader(fileName1);
      // buffer the input stream
    BufferedReader br1 =
        new BufferedReader(fr1);

      // set up input stream2
    FileReader fr2 = new
        FileReader(fileName2);
      // buffer the input stream
    BufferedReader br2 =
        new BufferedReader(fr2);

      // read and display1
    String buffer1 = "";

    ArrayList<String> firstFile1 = new ArrayList<String>();
    while ((buffer1 = br1.readLine()) != null) {
        firstFile1.add(buffer1);
      System.out.println(inLine1);  // display the line
  }
  br1.close();



    //Now read the second file or make for this separate method
      // read and display2
    String buffer2 = "";

    ArrayList<String> firstFile2 = new ArrayList<String>();
    while ((buffer2 = br2.readLine()) != null) {
        firstFile2.add(buffer2);
      System.out.println(inLine2);  // display the line
  }
  br2.close();

    //Read all the lines in array or list
    //After that you can calculate them.
 }

}
4

2 回答 2

0

阅读BufferedReader文档。您的文件未使用预期的行分隔符类型进行格式化。我建议使用 aScanner并将行分隔符设置为适当的模式,或者使用String.split

于 2013-04-24T21:51:55.667 回答
0

您有两个不同的变量,buffer1并且inline1. 由于您从未设置 的值inline1,因此它将始终为null

于 2013-04-24T21:52:36.953 回答