我需要帮助尝试阅读具有2010年和2000年人口普查的两个文件。我必须阅读这两个文件,然后找出这两个文件之间的人口增长。我一直对单身状态保持无效。我知道我对inLine1和inLine2都是null。
该文件看起来像这样
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.
}
}
答案 0 :(得分:0)
阅读BufferedReader
文档。您的文件未使用预期的行分隔符类型进行格式化。我建议使用Scanner
并将行分隔符设置为适当的模式,或使用String.split
答案 1 :(得分:0)
您有两个不同的变量,buffer1
和inline1
。由于您从未设置inline1
的值,因此它始终为null
。