0

我正在尝试将 txt 文件中的数字列表转换为数字数组。有 26 个数字,每个数字位于文本文件的不同行。我的代码是

import java.io.*;
public class rocket {

    public static void main(String[] args) throws IOException, FileNotFoundException
    {
        BufferedReader b = new BufferedReader(new InputStreamReader(new FileInputStream("/Users/Jeremy/Documents/workspace/altitude.txt")));
        String[] stringArray = new String[25];
        double[] doubleArray = new double[stringArray.length];
        for(int i=0; i<25; i++)
        {
            stringArray[i] = b.readLine();
            doubleArray[i] = Double.parseDouble(stringArray[i]);
        }
        for(int i = 0; i<doubleArray.length; i++)
        {
            System.out.println(doubleArray[i]);
        }
    }
}

但是每次我运行它时,我都会得到一个数字格式异常。如果我尝试只打印出字符串,我会得到一个 indexOutOfBounds 异常

4

4 回答 4

2

有问题你提到有26个字符串所以declate

String[] stringArray = new String[26];

并且当 readline 与 linbreak 返回时,会发生数字格式异常。要阅读行,您可以执行以下操作

 public String[] readLines(String filename) throws IOException {
        FileReader fileReader = new FileReader(filename);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        List<String> lines = new ArrayList<String>();
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            lines.add(line);
        }
        bufferedReader.close();
        return lines.toArray(new String[lines.size()]);
    }

所以按照这个逻辑,你可以通过

public static Double[] readLines(String filename) throws IOException {
    FileReader fileReader = new FileReader(filename);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    List<Double> lines = new ArrayList<Double>();
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        lines.add(Double.parseDouble(line));
    }
    bufferedReader.close();
    return lines.toArray(new Double[lines.size()]);
}
于 2013-03-23T05:30:51.263 回答
1
String[] stringArray = new String[26];

试试这个

于 2013-03-23T05:30:32.917 回答
1

尝试

    BufferedReader b = new BufferedReader(
            new InputStreamReader(
                    new FileInputStream(
                            "D:/git-repo/general/misc_test/src/java/com/greytip/cougar/module/test/v2/controller/so/dump/data.txt")));

    List<String> lines = new ArrayList<String>();

    String line = null;
    while ((line = b.readLine()) != null) {
        lines.add(line);
    }

    String[] stringArray = lines.toArray(new String[lines.size()]);
    double[] doubleArray = new double[stringArray.length];
    for (int i = 0; i < stringArray.length; i++) {
        doubleArray[i] = Double.parseDouble(stringArray[i]);
    }

    for (int i = 0; i < doubleArray.length; i++) {
        System.out.println(doubleArray[i]);
    }
于 2013-03-23T05:36:12.880 回答
0
    import java.io.*;
    public class Rocket {

        public static void main(String[] args) throws IOException, FileNotFoundException
        {
            BufferedReader b = new BufferedReader(new InputStreamReader(new FileInputStream("/Users/Jeremy/Documents/workspace/altitude.txt")));
            String[] stringArray = new String[25];
            double[] doubleArray = new double[stringArray.length];
            for(int i=0; i<25; i++)
            {
                stringArray[i] = b.readLine();
                try{
                    doubleArray[i] = Double.parseDouble(stringArray[i]);
                }catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
            for(int i = 0; i<doubleArray.length; i++)
            {
                System.out.println(doubleArray[i]);
            }
        }
    }

请在将字符串解析为双精度时处理异常。

于 2013-03-23T05:30:00.400 回答