【发布时间】:2016-02-13 08:28:59
【问题描述】:
邮政编码、城市、州、纬度、经度
邮政编码、城市、州、纬度、经度
我正在尝试使它能够打开一个具有这样格式的地址的文本文件,创建一个循环,按顺序实例化一个具有五个参数的新 ZipCode 对象,然后将该对象添加到 ArrayList myZips。
我有一种感觉,至少我的分隔符是错误的。
public void readZipCodeData(String filename){
Scanner inFS = null;
FileInputStream fileByteStream = null;
try{
// open the File and set delimiters
fileByteStream = new FileInputStream(filename);
inFS = new Scanner(fileByteStream);
inFS.useDelimiter(", *");
// continue while there is more data to read
while(inFS.hasNext()) {
// read five data elements
int zipCode = inFS.nextInt();
String city = inFS.next();
String state = inFS.next();
double latitude = inFS.nextDouble();
double longitude = inFS.nextDouble();
ZipCode z1 = new ZipCode(zipCode, city, state, latitude, longitude);
myZips.add(z1);
}
fileByteStream.close();
// Could not find file
}catch(FileNotFoundException error1) {
System.out.println("Failed to read the data file: " + filename);
// error while reading the file
}catch(IOException error2) {
System.out.println("Oops! Error related to: " + filename);
}
}
每次我尝试按原样运行它都会给我一个
java.util.InputMismatchException:
双经线出现 null(在 java.util.Scanner 中)错误。有什么想法吗?
【问题讨论】:
-
问题是什么?
-
糟糕,它切断了它,只需一秒钟
标签: java