【问题标题】:Trouble with importing files文件导入问题
【发布时间】: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


【解决方案1】:

我不熟悉Scanner 的输入,而是BufferedReader。我发现它使用起来很简单,我希望这个解决方案对你有用:

Charset charset = Charset.forName("US-ASCII");
try (BufferedReader reader = Files.newBufferedReader(filename, charset)) {
    String line = null;
    while ((line = reader.readLine()) != null) {

        // THIS IS THE CODE FOR EVERY LINE
        String[] data = line.split(", ");
        int zipCode = Integer.parseInt(data[0]);
        String city = data[1];
        String state = data[2];
        double latitude = Double.parseDouble(data[3]);
        double longitude = Double.parseDouble(data[4]);
        ZipCode z1 = new ZipCode(zipCode, city, state, latitude, longitude);
        myZips.add(z1);

    }
} catch (IOException x) {
    System.err.format("IOException: %s%n", x);
} catch (NumberFormatException x) {
    System.err.format("NumberFormatException: %s%n", x);
}

在这个例子中,我用BufferedReader.readLine() 读取了整行,并使用String.split()Integer.parseInt() / Double.parseDouble() 手动解析它。它更直观,而且有效!

working example here


由于下面的评论,我想我不能建议上面的答案。但是,我看到了两个潜在的问题:

  • 您可能没有在输入文件中输入double。简单而诚实的错误。
  • 或者,this SO answer,您的语言环境可能设置错误。在某些地方,他们使用, 代替. 来表示小数点尝试切换这些。

【讨论】:

  • 我的老师对使用提供给您的代码非常严格。如果我使用它,即使它是正确的,她也会变成猿屎。不知道为什么
  • @David 也许你可以看看我提供的例子?它可能会给你一些想法。因为您没有发布整个代码,所以其他地方可能存在错误。
  • 我能想到的唯一错误是输入——也许你没有输入双精度?
  • 很高兴我提供了帮助,但是现在新的错误/异常是什么?
  • 我需要弄清楚为什么 try 会赶上 }catch(FileNotFoundException error1)
【解决方案2】:

试试这样的。不要一次解析一个字段,而是抓取整行,使用分隔符, 将其转换为String 的数组,然后解析为int/doubleScanner.nextLine() 为您抓取整条线路。

try{
    // open the File and set delimiters
    fileByteStream = new FileInputStream(filename);
    inFS = new Scanner(fileByteStream);


        // read five data elements
            String[] data = inFS.nextLine().split(", ");
            int zipCode = Integer.parseInt(data[0]);
            String city = data[1];
            String state = data[2];
            double latitude = Double.parseDouble(data[3]);
            double longitude = Double.parseDouble(data[4]);
        ZipCode z1 = new ZipCode(zipCode, city, state, latitude, longitude);
        myZips.add(z1);

【讨论】:

    猜你喜欢
    • 2019-05-22
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2021-08-23
    • 2013-05-21
    • 2020-09-01
    相关资源
    最近更新 更多