我正在尝试从文件中读取行并将每个值单独存储在数组中。似乎可以很好地阅读前两个标题行,但随后对所有内容都返回 null 。我使用方法来转换一些变量,但即使当我从文件中读取纯 int 时,它也只会返回“0”,这是没有意义的,因为它似乎正在使用相同的基本读取文件而没有问题上一个项目中的方法(但没有数组)。这是我的代码:
package prog9;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Scanner;
public class Main {
private static String firstline;
private static String secondline;
public static void main(String[] args) throws IOException {
File file = new File("files/PortlandWeather2011.txt");
int count = 0;
BufferedReader reader2 = new BufferedReader(new FileReader(file));
while (reader2.readLine() != null)
count++;
reader2.close();
Scanner reader = new Scanner(file);
String prcp[] = new String[count];
String snow[] = new String[count];
String snwd[] = new String[count];
String tmin[] = new String[count];
String tmax[] = new String[count];
String station[] = new String[count];
String date[] = new String[count];
int test[] = new int[count];
firstline = reader.nextLine();
secondline = reader.nextLine();
System.out.println(firstline);
System.out.println(secondline);
int i = 0;
while (reader.hasNextLine()) {
station[i] = reader.next();
// test[i] = reader.nextInt();
date[i] = convertDate(reader.nextInt());
prcp[i] = convertTenthsToInches(reader.nextInt());
snow[i] = convertToInches(reader.nextInt());
snwd[i] = convertToInches(reader.nextInt());
tmax[i] = convertToDegrees(reader.nextInt());
tmin[i] = convertToDegrees(reader.nextInt());
// System.out.println(reader.nextLine());
i++;
if (reader.hasNextLine()) {
reader.nextLine();
}
}
reader.close();
for (int x = 0; x < count; x++) {
System.out.printf("%s %s %s %s %s %s %s\n", station[i], test[i], prcp[i], snow[i], snwd[i], tmax[i], tmin[i]);
}
}
// our first helper method, takes the date and converts it into the proper
// format as a string.
private static String convertDate(int d) {
// first make it into a date so we can strip the proper sections out of
// it
String date = "" + d;
// store the year, month, and day seperately so we can rearange them
String year = date.substring(0, 4);
String month = date.substring(4, 6);
String day = date.substring(6, 8);
// add the slashes and the day/month/year in the correct order.
String translated_date = month + "/" + day + "/" + year;
// spit the translated date out.
return translated_date;
}
// first conversion for inches, since some are in tenths and some are not.
private static String convertTenthsToInches(int i) {
// create a number format so we can limit the amount of decimal places
// cleanly.
String converted = null;
NumberFormat nf = NumberFormat.getNumberInstance();
// devide the input by 10 so its not in tenths anymore.
double input = i / 10.0;
// if its 0 we just want 0.0, so lets test that now.
if (input == 0) {
converted = "0.0";
} else {
// if its not 0 we want to set the maximum and minimum decimal
// places allowed, in this it would just be 1.
nf.setMaximumFractionDigits(1);
nf.setMinimumFractionDigits(1);
// and finally format it using our numberformat and devide by 25.4
// so we get inches instead of millimeters.
converted = nf.format((input / 25.4));
}
// returns our new number, and formats it so its padded to 8 characters.
return String.format("%8s", converted);
}
// our second inches helper method. for anything that isnt in tenths.
private static String convertToInches(int input) {
String converted = null;
NumberFormat nf = NumberFormat.getNumberInstance();
if (input == 0) {
converted = "0.0";
} else if (input == 9999) {
converted = "----";
} else {// exactly the same as before
nf.setMaximumFractionDigits(1);
nf.setMinimumFractionDigits(1);
converted = nf.format((input / 25.4));
}
return String.format("%8s", converted);
}
// and finally converting the tenths of C to F
private static String convertToDegrees(int i) {
String converted = null;
NumberFormat nf = NumberFormat.getNumberInstance();
double input = i / 10.0;
if (input == 0) {
converted = "0.0";
} else if (input == 9999) {
converted = "----";
} else {
nf.setMaximumFractionDigits(1);
nf.setMinimumFractionDigits(1);
// we convert the celsius to degrees using our formula
converted = nf.format((input * 9 / 5 + 32));
}
// and then format it so its padded to 8 characters and return it.
return String.format("%8s", converted);
}
}
该文件如下所示:
STATION DATE PRCP SNOW SNWD TMAX TMIN
----------------- ---------- -------- -------- -------- -------- --------
GHCND:USW00014764 20110101 0 0 127 122 -17
GHCND:USW00014764 20110102 5 0 102 67 28
GHCND:USW00014764 20110103 13 0 76 44 -56
GHCND:USW00014764 20110104 0 0 76 6 -83
GHCND:USW00014764 20110105 0 0 76 17 -83
GHCND:USW00014764 20110106 0 0 76 -11 -106
GHCND:USW00014764 20110107 0 0 76 -17 -122
GHCND:USW00014764 20110108 0 0 51 6 -78
GHCND:USW00014764 20110109 3 3 76 39 -44
GHCND:USW00014764 20110110 0 0 51 28 -67
GHCND:USW00014764 20110111 0 0 51 -6 -122
GHCND:USW00014764 20110112 185 330 76 0 -44
并且输出总是这样:
STATION DATE PRCP SNOW SNWD TMAX TMIN
----------------- ---------- -------- -------- -------- -------- --------
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
谁能解释为什么会发生这种情况?