将字符串数组转换为double数组

时间:2013-03-23 05:20:22

标签: java arrays parsing

我正在尝试将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 个答案:

答案 0 :(得分: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()]);
}

答案 1 :(得分:1)

String[] stringArray = new String[26];

试试这个

答案 2 :(得分: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]);
    }

答案 3 :(得分: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]);
            }
        }
    }

解析字符串时请处理异常请加倍。